我在Xamarin表格中有這樣一個標簽:
<Label HorizontalOptions="start" />
但是我希望HorizontalOptions的值可以根據我在后端代碼中輸入的字串變數(字串getH)而改變。
我試著用系結的方法,但它并不奏效。
<Label HorizontalOptions="{Binding
getH}"/> (XAML代碼)
getH = "End"; (C# code)。
在這種情況下,是否有任何系結方法可以發揮作用?
uj5u.com熱心網友回復:
HorizontalOptions的型別不是string,因此你不能系結到一個字串。它的型別是LayoutOptions,你應該把它系結到一個型別為LayoutOptions的屬性上,就像下面這樣,
public LayoutOptions GetH { get; set; } = LayoutOptions.Center;
然后在Xaml上,
<Label HorizontalOptions="{Binding GetH}"/span> />
否則,如果你因為某些原因希望系結到一個字串,那么你可以有一個轉換器。這方面已經有一個解決方案了這里。
uj5u.com熱心網友回復:
是的,如果你想系結string variabke,你可以使用converter來實作。
我創建了一個簡單的演示,你可以參考一下。
MyViewModel.cs
public class MyViewModel: INotifyPropertyChangedpublic ICommand BtnResetClickedCommand { private set; get; }
public MyViewModel() /span> {
GetH = "End";
BtnResetClickedCommand = new Command(ResetMethod)。
}
private void ResetMethodResetMethod(object obj)。
{
GetH = "Start"。
}
string _getH;
public string GetH
{
set { SetProperty(ref _getH, value) ; }
get { return _getH; }
}
bool SetProperty<T> (ref T storage, T value, [CallerMemberName] string propertyName = null).
{
if (Object.Equals(storage, value)
return false;
storage = value;
OnPropertyChanged(propertyName);
return true。
}
protected void OnPropertyChanged( [CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pertyName))。
}
public event PropertyChangedEventHandler PropertyChanged。
MyTextAlignmentConverter.cs
public class MyTextAlignmentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)。
{
if (value != null)
{
string valueAsString = value.ToString() 。
switch (valueAsString)
{
case ("EndAndExpand") 。
{
return LayoutOptions.EndAndExpand。
}
case ("StartAndExpand"):
{
return LayoutOptions.StartAndExpand。
}
case ("Center"):
{
return LayoutOptions.Center。
}
case ("End"):
{
return LayoutOptions.End。
}
case ("start"):
{
return LayoutOptions.Start。
}
default:
{
return LayoutOptions.StartAndExpand。
}
}
}
else[/span
{
return LayoutOptions.Center。
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)。
{
return null。
}
使用方法:
<?xml version="1.0" encoding="utf-8" ? >
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"。
xmlns:testbottomsheet="clr-namespace:TestBottomSheet"/span>
x:Class="TestBottomSheet.OptionPage"/span>>
<ContentPage.Resources>/span>
<ResourceDictionary >
<testbottomsheet:MyTextAlignmentConverter x:Key="mTextAlignmentConverter">/span>
</testbottomsheet:MyTextAlignmentConverter>
</ResourceDictionary>/span>
</ContentPage.Resources>
<ContentPage.BindingContext>/span>
<testbottomsheet:MyViewModel></testbottomsheet:MyViewModel>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand"/span>>
<Label Text="Welcome to Xamarin. BackgroundColor="Yellow"。
VerticalOptions="CenterAndExpand" HorizontalOptions="{Binding GetH,Converter={StaticResource mTextAlignmentConverter}}"
/>
< Button Text="reset" Command="{Binding BtnResetClickedCommand}}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>/span>
注意:
在上面的代碼中,我為viewmodel MyViewModel實作了介面INotifyPropertyChanged,并添加了一個reset按鈕。當我點擊這個按鈕時,我們也可以改變變數GetH的系結值并重繪 UI。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/312703.html
標籤:
