我目前正在 Xamarin.Forms XAML 中訓練一些系結和 MVVM。我有一個帶有網格的視圖,我想將整個視圖作為一個孩子系結到它。
此外,我正在使用 ZXing.Net.Mobile.Forms 及其要顯示的視圖。
以編程方式沒有問題
grid.Children.Add(MyView);
但是如何使用 XAML 中的系結實作相同的結果?
提前致謝!
視圖應系結到的我的 Xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ZxingNetMobileTemplate.ScannerView">
<Grid BindingContext="{Binding .}" x:Name="test">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*" ></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackLayout Orientation="Horizontal">
<Image Source="{Binding ImgSource}" HeightRequest="30" WidthRequest="30" HorizontalOptions="End" VerticalOptions="Start">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTorchTapped"></TapGestureRecognizer>
</Image.GestureRecognizers>
</Image>
</StackLayout>
</Grid>
</ContentPage>
我的 Xaml.cs 與視圖模型:
public partial class ScannerView : ContentPage
{
public CameraViewModel CameraViewModel { get; private set; }
public ScannerView(IEnumerable<BarcodeFormat> formats = null)
{
InitializeComponent();
BindingContext = CameraViewModel is null ? CameraViewModel = new CameraViewModel(formats) : CameraViewModel;
}
protected override void OnAppearing()
{
base.OnAppearing();
CameraViewModel.ScanAsync();
//test.Children.Add(CameraViewModel.Scanner);
//test.Children.Add(CameraViewModel.Overlay);
SubscribeScanResult();
CameraViewModel.Scanner.IsAnalyzing = true;
CameraViewModel.Scanner.IsScanning = true;
}
}
...
最后但并非最不重要的是我的視圖模型,其中屬性“掃描儀”和“覆寫”應該系結到 xaml
public class CameraViewModel
{
public ZXingScannerView Scanner { get; private set; }
public ZXingDefaultOverlay Overlay { get; private set; }
public IEnumerable<BarcodeFormat> CustomFormats { get; private set; }
public string ImgSource => Device.RuntimePlatform == Device.Android ? "thunder.png" : "Images/thunder.png";
public CameraViewModel(IEnumerable<BarcodeFormat> formats)
{
CustomFormats = formats;
}
...
}
uj5u.com熱心網友回復:
制作自定義“網格”
XAML
<ContentView
....>
<ContentView.Content>
<Grid x:Name="Grid">
.... here is your default content
</Grid>
</ContentView.Content>
</ContentView>
。CS
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View1 : ContentView
{
public static readonly BindableProperty CustomViewProperty = BindableProperty.Create(nameof(CustomView), typeof(View), typeof(View1), null, propertyChanged: OnCustomViewChanged);
public View1()
{
InitializeComponent();
}
public View CustomView
{
get { return (View)GetValue(CustomViewProperty); }
set { SetValue(CustomViewProperty, value); }
}
static void OnCustomViewChanged(BindableObject bindable, object oldValue, object newValue)
{
//x:name Grid xaml
//not sure if you want to clear grid before add new control
//((View1)bindable).Grid.Children.Clear();
((View1)bindable).Grid.Children.Add((View)newValue);
}
}
然后在您的 Xaml 而不是 Grid 中使用 View1(在我的情況下,請選擇一些更好的名稱 :))。
//not sure if you want to bind Scanner property
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ZxingNetMobileTemplate.ScannerView">
<View1 CustomView="{Binding Scanner}">
</ContentPage>
注意:擁有包含視圖的 ViewModel 不是一個好習慣。想象一下,你想在不同的應用程式中使用這個 ViewModel,比如說 WPF。你不能,因為它依賴于 Zxing。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415015.html
標籤:
上一篇:在wpf中單擊時更改按鈕的顏色
