[英]Change visibility of a UWP button from code behind
I have a "master frame" (which contains only the CommandBar) and some child frames, which are initially in a hub. 我有一个“主框架”(只包含CommandBar)和一些子框架,它们最初位于集线器中。 Basically the frame changes, when one hub element is clicked on via "OnNavigated.." 当通过“OnNavigated ..”点击一个hub元素时,框架基本上会发生变化。
Now I have some buttons (for example 1 and 2) which should not be visible, only when certain frames are chosen: 现在我有一些按钮(例如1和2),只有在选择某些帧时才能看到它们:
I've tried it with getter and setter methods: 我用getter和setter方法尝试过:
In the Master-Frame code-methods: 在Master-Frame代码方法中:
public static Visibility setVisibility
{
set { Button1.Visibility = value; }
}
and in the Frame1 code behind: 并在后面的Frame1代码中:
MasterFrame.setVisibility = Visibility.Visible;
But I'm getting the error from Button1 "An object reference is...", because I have to use the "static" modifier to get access to the button from Frame1. 但我从Button1“对象引用是......”得到错误,因为我必须使用“静态”修饰符来访问Frame1中的按钮。
How can I get access to the button? 如何才能访问该按钮?
I don't even know if I'm using the "right" approach with the code-behind, but the MVVM seems to be not useful, as this isn't a CRUD-application (simple information without user-input.) 我甚至不知道我是否正在使用代码隐藏的“正确”方法,但MVVM似乎没用,因为这不是CRUD应用程序(没有用户输入的简单信息。)
I don't even know if I'm using the "right" approach with the code-behind, but the MVVM seems to be not useful, as this isn't a CRUD-application (simple information without user-input.) 我甚至不知道我是否正在使用代码隐藏的“正确”方法,但MVVM似乎没用,因为这不是CRUD应用程序(没有用户输入的简单信息。)
No, MVVM is useful, in the MVVM design pattern, developers can code app logic, and designers can create the UI. 不,MVVM很有用,在MVVM设计模式中,开发人员可以编写应用程序逻辑,设计人员可以创建UI。 Although you're not developing a CRUD-application, MVVM pattern can still be used. 虽然您没有开发CRUD应用程序,但仍可以使用MVVM模式。
In a UWP app, Data Binding is very powerful. 在UWP应用程序中, 数据绑定非常强大。 In this case, you can use data binding together with Converter to solve your problem. 在这种情况下,您可以将数据绑定与Converter一起使用来解决您的问题。
I wrote a sample here to use Data Binding for event, and use Converter
to judge the Visibility of Button
and AppBarButton
s: 我在这里写了一个示例,使用Data Binding for event,并使用Converter
来判断Button
和AppBarButton
的可见性:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<Converter:VisiableOrNot x:Key="cvt" />
<Converter:NaviButtonShowOrNot x:Key="btncvt" />
</Grid.Resources>
<CommandBar>
<CommandBar.Content>
<Grid>
<TextBlock Text="Master-Frame" FontSize="20" Margin="20,10" />
</Grid>
</CommandBar.Content>
<AppBarButton Icon="Accept" Label="appbarbutton" Visibility="{Binding ElementName=mainPageframe, Path=Content.BaseUri.AbsoluteUri, Converter={StaticResource cvt}}" />
<AppBarButton Icon="Cancel" Label="appbarbutton" Visibility="{Binding ElementName=mainPageframe, Path=Content.BaseUri.AbsoluteUri, Converter={StaticResource cvt}}" />
</CommandBar>
<Frame x:Name="mainPageframe" Margin="0,55">
<Hub x:Name="hub" SectionHeaderClick="{x:Bind MainPageViewModel.hub_SectionHeaderClick}">
<HubSection x:Name="image1" Header="Image1" Width="200" IsHeaderInteractive="True">
<DataTemplate>
<Grid>
<Image Source="Assets/111.png" Stretch="None" />
</Grid>
</DataTemplate>
</HubSection>
<HubSection x:Name="image2" Header="Image2" Width="200" IsHeaderInteractive="True">
<DataTemplate>
<Grid>
<Image Grid.Row="0" Source="Assets/222.png" Stretch="None" />
</Grid>
</DataTemplate>
</HubSection>
<HubSection x:Name="image3" Header="Image3" Width="200" IsHeaderInteractive="True">
<DataTemplate>
<Grid>
<Image Source="Assets/333.png" Stretch="None" />
</Grid>
</DataTemplate>
</HubSection>
</Hub>
</Frame>
<Button Content="Go Back" Click="{x:Bind MainPageViewModel.Button_Click}" Background="PaleGreen" VerticalAlignment="Bottom" Margin="50,20" Visibility="{Binding ElementName=mainPageframe, Path=Content.BaseUri.AbsoluteUri, Converter={StaticResource btncvt}}" />
</Grid>
Code of VisiableOrNot
converter: VisiableOrNot
转换器代码:
public class VisiableOrNot : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
Uri uri = new Uri(value.ToString());
if (uri != null)
{
if (uri.Equals("ms-appx:///View/Page3.xaml"))
{
return Visibility.Visible;
}
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Here is the rendering image of my demo, the AppBarButton
s can be seen only when the child frame's content is Page3. 这是我的演示的渲染图像,只有当子帧的内容是AppBarButton
时才能看到AppBarButton
。 And the navigate back button can not be seen when it is on MainPage: 当它在MainPage上时,无法看到导航后退按钮:
Here is my demo , you can download it and have a check. 这是我的演示 ,您可以下载它并进行检查。
To hide a ui element just do this: 要隐藏ui元素,只需执行以下操作:
this.MyComponent.Visibility = Visibility.Collapsed;
And to make it visible do this: 为了使其可见,请执行以下操作:
this.MyComponent.Visibility = Visibility.Visible;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.