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

如何在.NET MAUI中正确将HomeViewModel传递给自定义ContentView?

.NET MAUI 控件传递ViewModel的正确实现方案

问题根源

你的代码里有两个关键问题导致MyControl无法获取HomeViewModel且内容不可见:

  1. MyControl硬绑定了自身为BindingContext:在MyControl的XAML中,你设置了<ContentView.BindingContext><x:Reference Name="this"/></ContentView.BindingContext>,这直接覆盖了从HomePage传递过来的HomeViewModel,控件自然无法绑定到ViewModel的属性。
  2. Frame完全透明:Frame的Opacity="0"会让整个容器及内部内容完全不可见,即使绑定正确也看不到东西。

修复步骤

1. 移除MyControl的硬编码BindingContext

打开MyControl.xaml,删除以下代码块:

<ContentView.BindingContext>
  <x:Reference Name="this" />
</ContentView.BindingContext>

删除后,MyControl会自动继承父容器(HomePage)的BindingContext,也就是HomeViewModel。

2. 修复Frame的Opacity属性

将MyControl中Frame的Opacity="0"改为Opacity="1"(或者直接移除这个属性,默认就是1):

<Frame BackgroundColor="{Binding ThemeColor}">

3. 可选:通过BindableProperty明确传递ViewModel(更灵活的方案)

如果希望控件的复用性更强,可以给MyControl添加一个专门的ViewModel绑定属性:

MyControl.xaml.cs:

public partial class MyControl : ContentView
{
    public static readonly BindableProperty ViewModelProperty =
        BindableProperty.Create(nameof(ViewModel), typeof(IHomeViewModel), typeof(MyControl));

    public IHomeViewModel ViewModel
    {
        get => (IHomeViewModel)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, value);
    }

    public MyControl()
    {
        InitializeComponent();
        this.BindingContext = this;
    }
}

MyControl.xaml:
修改x:DataType为自身,并调整绑定路径:

<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewModels="clr-namespace:MyApp.ViewModels"
             xmlns:models="clr-namespace:MyApp.Models"
             xmlns:controls="clr-namespace:MyApp.Controls"
             x:Name="this"
             x:Class="MyApp.Controls.MyControl"
             x:DataType="controls:MyControl">

  <Grid>
    <Frame BackgroundColor="{Binding ViewModel.ThemeColor}">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Label Text="Title" FontSize="14" Grid.Row="0"></Label>
          <ListView ItemsSource="{Binding ViewModel.Data}" Grid.Row="1" SelectedItem="{Binding ViewModel.SelectedItem}">
            <ListView.ItemTemplate>
              <DataTemplate x:DataType="models:DataModel">
                <ViewCell>
                  <Grid Padding="5">
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*"/>
                      <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Text="{Binding Subject}"/>
                    <Label Grid.Column="1" Text="{Binding Number}"/>
                  </Grid>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>
        </Grid>
    </Frame>
  </Grid>
</ContentView>

HomePage.xaml:
绑定MyControl的ViewModel属性到HomePage的BindingContext:

<controls:MyControl Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" 
                    ViewModel="{Binding}" />

验证修复

完成上述修改后,HomeViewModel会正确传递到MyControl中,Frame的内容也会正常显示。确保HomeViewModel中的Data集合已经正确初始化并填充了数据,这样ListView才能展示内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:55:07