【问题标题】:Getting "Can not find the object referenced by" error during simplest Xamarin data binding在最简单的 Xamarin 数据绑定期间出现“找不到引用的对象”错误
【发布时间】:2019-11-27 18:06:08
【问题描述】:

我对 Xamarin 开发有些陌生。尝试完成一个相当简单的任务:在 XAML 中,将菜单页面数据绑定到一个 ViewModel,该 ViewModel 被注入到我的菜单页面中。

这就是我的 XAML 的样子。 Intellisense 识别 _viewModel 并在 ListView 中进一步显示其属性

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:d="http://xamarin.com/schemas/2014/forms/design"
            xmlns:mc="http://schemas.openxlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            BindingContext="{x:Reference_viewModel}"
            x:Class="SFC.Cliente.Mobile.Views.MenuPage"
            x:Name="Menu"
            Title="Menu">
  <StackLayout VerticalOptions="FillAndExpand">
     <ListView x:Name="ListViewMenu" HasUnevenRows="True" ItemsSource="{Binding MenuItems}">
        <ListView.ItemTemplate>
           <DataTemplate>
              <ViewCell>
                 <Grid Padding="10">
                    <Label Text="{Binding Title}" FontSize="20"/>
                 </Grid>
              </ViewCell>
           </DataTemplate>
        </ListView.ItemTemplate>
     </ListView>
  </StackLayout>
</ContentPage>

我的代码如下所示。 ViewModel 被注入到页面的代码隐藏中没有问题并且不为空

namespace SFC.Client.Mobile.Views
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(visible:false)]
    public partial class MenuPage : ContentPage
    {
        // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
        private readonly MenuItemsViewModel _viewModel;

        public MenuPage(MenuItemsViewModel viewModel)
        {
            _viewModel = viewModel;

            InitializeComponent();

            ListViewMenu.ItemSelected += async (sender, e) =>
            {
                if (e.SelectedItem == null)
                    return;

                var id = ((HomeMenuItem) e.SelectedItem).Id;
                var rootPage = (MainPage) Application.Current.MainPage;
                await rootPage.NavigateFromMenu(id);
            };
        }
    }
}

这是我在 InitializeComponent() 期间遇到的异常。我想通过 XAML 连接数据绑定,而不是通过代码。我尝试将 _viewModel 设为公共或私有、财产或成员:没有变化。我做错了什么?

{System.Collections.ListDictionaryInternal}
-2146233088
(空)
(空)
"位置 7:14。找不到_viewModel 引用的对象"
"Xamarin.Forms.Xaml"
" 在 Xamarin.Forms.Xaml.Reference.ProvideValue(System.IServiceProvider serviceProvider) [0..."
{System.Reflection.MonoMethod}

【问题讨论】:

  • 为什么是图片而不是代码?
  • 因为即使使用 Ctrl-K,SO 也不允许我通过 XAML,所以我放弃了尝试理解原因并粘贴屏幕截图 :) 过去 2 天我一直在处理这个问题并阅读每篇数据绑定文章并观看每一个视频......沮丧
  • @Igorek 也许Set BindingContext to ViewModel 的帖子有用吗?
  • 我已经编辑了问题以删除图像并将它们放入文本中。每个人的数据都会很高兴。
  • 您应该能够将页面的 BindingContext 投射到您的虚拟机

标签: c# xaml xamarin mvvm xamarin.forms


【解决方案1】:

我必须同意上面的 Ivan Ičin。

我不知道为什么在 XAML 上设置绑定上下文如此重要,因为您可以轻松地做到这一点:

public partial class MenuPage : ContentPage
{
    // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
    // private readonly MenuItemsViewModel _viewModel;

    public MenuPage(MenuItemsViewModel viewModel)
    {
        BindingContext = viewModel; // <---------- changed line

        ...
    }
}

更新:找到了可能的解决方案。

在您的页面上设置静态属性,例如:

public static MenuItemsViewModel BindingContextInstance { get; set; } = null;

在调用InitializeComponent()之前,将您的视图模型分配给上述静态成员:

public MenuPage(MenuItemsViewModel viewModel)
{
    BindingContextInstance = viewModel;
    InitializeComponent();
      ...
} 

然后在 XAML 中,添加一个新的 xmlns 条目并设置 BindingContext:

xmlns:views="clr-namespace:SFC.Client.Mobile.Views"
BindingContext="{x:Static views:MenuPage.BindingContextInstance}"

注意:这不适用于当前稳定版本的 Visual Studio for Mac (8.1.5.9) 中的自动完成功能,但适用于当前的 Preview version (8.2) 和当前版本的 Visual Studio 2019 (16.1.6 ) 在 Windows 上。在 Visual Studio 更新对话框中将更新通道设置为在 Visual Studio for Mac 中预览,下载更新后,重新启动并安装更新。

【讨论】:

  • 因为我想要 xaml 中的智能感知支持?
  • 更新了答案。找到解决方案。
  • @Igorek ^^^^^^^
  • 最终这样做了。完美运行。谢谢
【解决方案2】:

我不认为你可以这样绑定。下面是如何在 XAML 中绑定视图模型的示例:Set BindingContext to ViewModel in XAML on Xamarin.Forms

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2019-05-11
    • 1970-01-01
    • 2018-02-16
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多