我對 Xamarin Forms 非常陌生,而且我非常迷茫。有人可以給我一個使用 MVVM 將主要資料系結到標簽的示例嗎?
我目前有InformationPageViewModel ofInformationPageModel和 Model of的視圖ResourceGatheringLogic。
我試圖弄清楚如何從嵌入式資源中讀取 JSON,但我什至無法正確系結字串以顯示HelloWorld資料系結。
我只想要一個非常簡單的例子來說明如何正確地做到這一點,我很可能會做剩下的。提前謝謝了。
我的.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:uCue_Game.ViewModel"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="uCue_Game.View.InformationPage">
<ContentPage.BindingContext>
<local:InformationPageModel/>
</ContentPage.BindingContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Frame BackgroundColor="LightGreen"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<Label Text="{Binding MediaSource}"
TextColor="Black"
FontSize="20"
FontAttributes="Bold"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
HeightRequest="300"
WidthRequest="300"/>
</Frame>
</Grid>
</ContentPage>
我的.xaml.cs
namespace uCue_Game.View
{
using Xamarin.Forms;
public partial class InformationPage : ContentPage
{
public InformationPage()
{
InitializeComponent();
}
}
}
我的 ViewModel.cs忽略 SetMediaSource 供以后使用。
namespace uCue_Game.ViewModel
{
using global::Model;
using Xamarin.Forms;
public class InformationPageModel
{
IMediaSource mediaSource;
public string MediaSource = "Hello";
public InformationPageModel()
{
this.mediaSource = DependencyService.Get<IMediaSource>();
//SetMediaSource();
}
public void SetMediaSource()
{
if (mediaSource != null)
return;
MediaSource = mediaSource.GetMediaSource();
}
}
}
uj5u.com熱心網友回復:
您只能系結到公共屬性
public string MediaSource { get; set; } = "Hello";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/510767.html
