c# - 在 Xamarin.Forms 中,如果您无法从 XAML 实例化 BindingContext,如何允许自动完成 XAML 代码中的绑定(bind)?

标签 c# xaml mvvm xamarin.forms

给定以下项目:

主页.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="A.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:A">
    <ContentPage.Content>
        <StackLayout>
            <StackLayout>
                <Button Clicked="Greet" Text="Greet" />
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

MainPage.xaml.cs

using System;
using Xamarin.Forms;

namespace A
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Greet(object sender, EventArgs e)
        {
            Navigation.PushModalAsync(new SubPage
            {
                BindingContext = new SubPageViewModel("World")
            });
        }
    }
}

子页面.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="A.SubPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentPage.Content>
        <StackLayout>
            <Label
                HorizontalOptions="CenterAndExpand"
                Text="{Binding Greeting}"
                VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

子页面.xaml.cs

using Xamarin.Forms;

namespace A
{
    public partial class SubPage : ContentPage
    {
        public SubPage ()
        {
            InitializeComponent ();
        }
    }
}

子页面 View 模型.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace A
{
    public class SubPageViewModel : INotifyPropertyChanged
    {
        private string place;

        public string Place
        {
            get => place;
            set
            {
                if(place != value)
                    return;
                place = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(Greeting));
            }
        }

        public SubPageViewModel(string place)
        {
            this.place = place;
        }

        public string Greeting => $"Hello, {place}";

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Visual Studio 不会自动完成 SubPage.xaml 中的绑定(bind),并且会提示它“由于未知的 DataContext 无法解析符号‘Greeting’”,因为语法突出显示在编译时不知道类型。通常,我会使用以下语法:

<ContentPage.BindingContext>
    <a:SubPageViewModel />
</ContentPage.BindingContext>

但是,这在这里不适用,因为 ViewModel 不是默认可构造的。

如果这是 WPF,我会同时使用 mc:Ignorable="d"d:DesignInstance,但是,DesignInstance似乎没有在 XF 中实现。

如何使自动完成工作?

最佳答案

您可以声明一个将在绑定(bind)中使用的虚拟静态属性。

子页面.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="A.SubPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:a="clr-namespace:A;assembly=A.Android"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    BindingContext="{x:Static a:SubPage.BindingContextDummyInstance}"> <!-- <------ added -->
    <ContentPage.Content>
        <StackLayout>
            <Label
                HorizontalOptions="CenterAndExpand"
                Text="{Binding Greeting}"
                VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

子页面.xaml.cs

using Xamarin.Forms;

namespace A
{
    public partial class SubPage : ContentPage
    {
        public static SubPageViewModel BindingContextDummyInstance => null; // <------ added

        public SubPage ()
        {
            InitializeComponent ();
        }
    }
}

这在代码语义方面没有任何改变,因为默认的 BindingContext 仍然是 null,但现在自动完成工作正常,因为 BindingContext 是在编译时已知。

关于c# - 在 Xamarin.Forms 中,如果您无法从 XAML 实例化 BindingContext,如何允许自动完成 XAML 代码中的绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47295881/

相关文章:

c# - 如何从代码隐藏中触发 jQuery 函数?

c# - TabControl 中所有 TabItem 的内容相同

c# - 这是使用MVVM Light Messenger类的正确方法吗

c# - 为什么 IDataErrorInfo 在应用程序启动时显示错误

c# - 如何更改 TabbedPage 中的默认选定项?

c# - LINQ Select with join 和 optional where

C#.NET - 无法添加或更新子行 : a foreign key constraint fails

c# - 如何在多个线程之间进行通信?

c# - 在 XAML 中操作静态资源

wpf - 我们可以在模板绑定(bind)时操作(减去)属性的值吗?