C# 我可以从ContentPage中切换到VM中的bool吗
我尝试在内容页中设置bool,以便在ViewModel中执行功能。我试图在内容页上按一个按钮来设置bool。然后我需要视图模型知道它设置为true。我之所以这样做是因为我需要交换按钮的功能 我需要交换这两个,以防我的问题毫无意义,也许这会让问题更清楚C# 我可以从ContentPage中切换到VM中的bool吗,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我尝试在内容页中设置bool,以便在ViewModel中执行功能。我试图在内容页上按一个按钮来设置bool。然后我需要视图模型知道它设置为true。我之所以这样做是因为我需要交换按钮的功能 我需要交换这两个,以防我的问题毫无意义,也许这会让问题更清楚 <Label x:Name="makeLargerEN" FontFamily="{StaticResource IconsFontFamily}" HorizontalTextAlignment="End"
<Label
x:Name="makeLargerEN"
FontFamily="{StaticResource IconsFontFamily}"
HorizontalTextAlignment="End"
Text="{Binding MakeLargerEN}"
TextColor="{DynamicResource AccentColor}"
VerticalTextAlignment="End">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding MakeWebViewLargeCommand }" />
</Label.GestureRecognizers>
<Label.FontSize>
<OnIdiom
x:TypeArguments="x:Double"
Phone="25"
Tablet="30" />
</Label.FontSize>
</Label>
<Label
x:Name="makeLargerEN"
FontFamily="{StaticResource IconsFontFamily}"
HorizontalTextAlignment="End"
Text="{Binding MakeLargerEN}"
TextColor="{DynamicResource AccentColor}"
VerticalTextAlignment="End">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding MakeWebViewLargeCommandEN }" />
</Label.GestureRecognizers>
<Label.FontSize>
<OnIdiom
x:TypeArguments="x:Double"
Phone="25"
Tablet="30" />
</Label.FontSize>
</Label>
public async void MakeWebViewLarge()
{
if (IsCz)
{
var popup = new WordAndPhrasePopup(htmlSourceExplanation.Html);
await PopupNavigation.Instance.PushAsync(popup);
}
else
{
var popup = new WordAndPhrasePopup(htmlSourceExplanation.Html);}
}
<ToolbarItem
x:Name="btnChangePositions"
Priority="0" Clicked="ChangePosition"
Text="{Binding ChangePositionsButton}" />
ContentPage / this is where i am trying to set the bool
public Dictionary()
{
InitializeComponent();
BindingContext = new DictionaryViewModel();
btnChangePositions.Text = "CZ/EN";
viewModel = new DictionaryViewModel();
viewModel.IsCz = true;
}
public bool isSwap = false;
private void ChangePosition(object sender, EventArgs e)
{
isSwap = !isSwap;
czView.RemoveBinding(ExtendedWebView.SourceProperty);
enView.RemoveBinding(ExtendedWebView.SourceProperty);
if (isSwap)
{
viewModel.IsCz = false;
btnChangePositions.Text = "EN/CZ";
czView.SetBinding(ExtendedWebView.SourceProperty,("EN"));
enView.SetBinding(ExtendedWebView.SourceProperty,("CZ"));
}
else
{
viewModel.IsCz = true;
btnChangePositions.Text = "CZ/EN";
czView.SetBinding(ExtendedWebView.SourceProperty, ("CZ"));
enView.SetBinding(ExtendedWebView.SourceProperty, ("EN"));
}
}
公共异步void MakeWebViewLarge()
{
如果(IsCz)
{
var popup=新单词和短语popup(htmlsourceexplation.Html);
等待PopupNavigation.Instance.PushAsync(弹出窗口);
}
其他的
{
var popup=新单词和短语popup(htmlsourceexplainion.Html);}
}
ContentPage/这就是我试图设置bool的地方
公共词典()
{
初始化组件();
BindingContext=newDictionaryViewModel();
btnChangePositions.Text=“CZ/EN”;
viewModel=新字典viewModel();
viewModel.IsCz=true;
}
public bool isSwap=false;
私有void ChangePosition(对象发送方,事件参数e)
{
isSwap=!isSwap;
RemoveBinding(ExtendedWebView.SourceProperty);
RemoveBinding(ExtendedWebView.SourceProperty);
如果(isSwap)
{
viewModel.IsCz=false;
btnChangePositions.Text=“EN/CZ”;
SetBinding(ExtendedWebView.SourceProperty,(“EN”);
SetBinding(ExtendedWebView.SourceProperty,(“CZ”);
}
其他的
{
viewModel.IsCz=true;
btnChangePositions.Text=“CZ/EN”;
SetBinding(扩展的webview.SourceProperty,(“CZ”);
enView.SetBinding(ExtendedWebView.SourceProperty,(“EN”);
}
}
您正在创建两个不同的VM实例-您的XAML使用的是BindingContext
,而您的代码隐藏使用的是viewModel
InitializeComponent();
BindingContext = new DictionaryViewModel();
btnChangePositions.Text = "CZ/EN";
viewModel = new DictionaryViewModel();
viewModel.IsCz = true;
而是使用两个不同的引用创建一个实例
InitializeComponent();
BindingContext = viewModel = new DictionaryViewModel();
btnChangePositions.Text = "CZ/EN";
viewModel.IsCz = true;
请为视图(xaml)、代码隐藏(xaml.cs)和视图模型将代码分段。你的代码很难读