![](/img/trans.png)
[英]how to upper/lower case resx bindings in xaml in xamarin.forms?
[英]In Xamarin.Forms, how to allow for autocompletion for bindings in XAML code in the case you can't instantiate the BindingContext from XAML?
鑒於以下項目:
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">
<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")
});
}
}
}
SubPage.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>
SubPage.xaml.cs
using Xamarin.Forms;
namespace A
{
public partial class SubPage : ContentPage
{
public SubPage ()
{
InitializeComponent ();
}
}
}
SubPageViewModel.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,我將使用mc:Ignorable="d"
和d:DesignInstance
,但是, DesignInstance
似乎沒有在XF中實現。
如何使自動完成工作?
您可以聲明將在綁定中使用的虛擬靜態屬性。
SubPage.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>
SubPage.xaml.cs
using Xamarin.Forms;
namespace A
{
public partial class SubPage : ContentPage
{
public static SubPageViewModel BindingContextDummyInstance => null; // <------ added
public SubPage ()
{
InitializeComponent ();
}
}
}
這對代碼語義沒有任何改變,因為默認的BindingContext
仍為null
,但現在自動完成正在工作,因為BindingContext
在編譯時是已知的。
你可以試試:
<ContentPage.BindingContext>
<x:Type Type="a:SubPageViewModel " />
</ContentPage.BindingContext>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.