I'm incredibly new to Xamarin Forms and I'm super lost. Could someone please give me an example of primary data binding to a label using MVVM?
I currently have View of InformationPage
ViewModel of InformationPageModel
and Model of ResourceGatheringLogic
.
I was trying to figure out how to read JSON from embedded resources but I can't even properly bind a string to display HelloWorld
for data binding.
I would only like a very simple example of how to properly do this and I can most likely do the rest. Many Thanks in advance.
My .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>
My .xaml.cs
namespace uCue_Game.View
{
using Xamarin.Forms;
public partial class InformationPage : ContentPage
{
public InformationPage()
{
InitializeComponent();
}
}
}
My ViewModel .cs
Ignore the SetMediaSource it is for later use.
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();
}
}
}
1条答案
按热度按时间tjjdgumg1#
只能绑定到公共属性