C# 在Xamarin.Forms中,如果您可以';是否从XAML实例化BindingContext?
鉴于以下项目: MainPage.xamlC# 在Xamarin.Forms中,如果您可以';是否从XAML实例化BindingContext?,c#,xaml,mvvm,xamarin.forms,C#,Xaml,Mvvm,Xamarin.forms,鉴于以下项目: MainPage.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
<?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>
子页面.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>
<?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>
子页面viewmodel.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中的绑定,并会抱怨它“由于未知的DataContext而无法解析符号“问候语”,因为语法高亮显示在编译时不知道类型。通常,我会使用以下语法:
<ContentPage.BindingContext>
<a:SubPageViewModel />
</ContentPage.BindingContext>
但是,这在这里不适用,因为ViewModel不是默认可构造的
如果这是WPF,我将与d:DesignInstance
一起使用mc:Ignorable=“d”
,但是,DesignInstance
似乎没有在XF中实现
如何使自动完成工作?您可以声明将在绑定中使用的虚拟静态属性 子页面.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>
<?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 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")
});
}
}
}
using Xamarin.Forms;
namespace A
{
public partial class SubPage : ContentPage
{
public SubPage ()
{
InitializeComponent ();
}
}
}
using Xamarin.Forms;
namespace A
{
public partial class SubPage : ContentPage
{
public static SubPageViewModel BindingContextDummyInstance => null; // <------ added
public SubPage ()
{
InitializeComponent ();
}
}
}
使用Xamarin.Forms;
名称空间A
{
公共部分类子页面:ContentPage
{
公共静态子页面ViewModel BindingContextDummyInstance=>null;//您可以尝试:
<ContentPage.BindingContext>
<x:Type Type="a:SubPageViewModel " />
</ContentPage.BindingContext>
未编译(位置9:17。未找到“Type”的属性、可绑定属性或事件,或值和属性之间的类型不匹配。
),但令人惊讶的是,它会导致自动完成工作。太奇怪了:/I有xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml"
不知道这是否有什么不同。我还在WPF应用程序上进行了测试。这在当前稳定版本的Visual Studio for Mac(8.1.5.9)中不起作用,但在Windows上的当前预览版本(8.2)和Visual Studio 2019(16.1.6)中起作用。