You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI C#跨项目绑定对象至DLL视图的方法求助

MAUI与DLL项目间实现列表数据绑定的正确步骤

1. 在DLL的自定义列表视图中声明可绑定属性

首先要在DLL的列表控件代码文件(比如CustomListView.xaml.cs)里,定义一个支持数据绑定的属性,用来接收MAUI项目传递的列表数据:

using System.Collections.ObjectModel;
using Microsoft.Maui.Controls;

namespace YourDllNamespace;

public partial class CustomListView : ContentView
{
    // 声明可绑定属性,用ObservableCollection确保数据更新能通知UI
    public static readonly BindableProperty ItemsSourceProperty =
        BindableProperty.Create(
            nameof(ItemsSource),
            typeof(ObservableCollection<ItemModel>),
            typeof(CustomListView),
            new ObservableCollection<ItemModel>());

    public ObservableCollection<ItemModel> ItemsSource
    {
        get => (ObservableCollection<ItemModel>)GetValue(ItemsSourceProperty);
        set => SetValue(ItemsSourceProperty, value);
    }

    public CustomListView()
    {
        InitializeComponent();
    }
}

然后在DLL的CustomListView.xaml中,把内部ListView的ItemsSource绑定到这个自定义属性:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourDllNamespace.CustomListView"
             x:Name="ThisView">
    <ListView ItemsSource="{Binding ItemsSource, Source={x:Reference ThisView}}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <!-- 自定义列表项样式,确保绑定路径和ItemModel的属性一致 -->
                <ViewCell>
                    <Label Text="{Binding ItemName}" />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentView>

2. MAUI项目中正确传递数据

在MAUI的MainViewModel里,确保列表数据是ObservableCollection类型(普通List无法触发UI更新):

using System.Collections.ObjectModel;

namespace YourMauiNamespace;

public class MainViewModel
{
    public ObservableCollection<ItemModel> listItems { get; set; }

    public MainViewModel()
    {
        // 初始化并填充数据
        listItems = new ObservableCollection<ItemModel>
        {
            new ItemModel { ItemName = "测试项1" },
            new ItemModel { ItemName = "测试项2" }
        };
    }
}

// ItemModel要让两个项目都能访问,可放在DLL或共享类库中
public class ItemModel
{
    public string ItemName { get; set; }
}

接着在MainPage.xaml中导入DLL的命名空间,并绑定自定义控件的ItemsSource:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dll="clr-namespace:YourDllNamespace;assembly=YourDllAssemblyName"
             x:Class="YourMauiNamespace.MainPage">

    <dll:CustomListView ItemsSource="{Binding listItems}" />
</ContentPage>

最后在MainPage.xaml.cs中设置绑定上下文:

namespace YourMauiNamespace;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext = new MainViewModel();
    }
}

3. 排查常见问题

  • 确认MAUI项目已正确引用DLL项目,XAML中的命名空间声明(xmlns:dll)要和DLL的命名空间、程序集名匹配。
  • 确保ItemModel的属性是公共可访问的,且绑定路径拼写正确。
  • 避免用普通List做绑定数据源,它无法触发UI更新,始终优先使用ObservableCollection。

内容的提问来源于stack exchange,提问作者VincentS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 22:23:26