简体   繁体   English

C#UWP XAML检查XAML中是否存在

[英]C# UWP XAML Check If Exists in XAML

Im working on building different layouts for my app. 我正在为我的应用程序构建不同的布局。 If the app runs in landscape I want to be able to show a map on one side on the screen, but not show a map if in portrait. 如果该应用程序在横向运行,我希望能够在屏幕的一侧显示地图,但在纵向显示时不显示地图。 I'm using VisualStateManager to help me do this. 我正在使用VisualStateManager来帮助我做到这一点。

I need to run some code in c# on the map control I have named "MyMap" but how can I make that bit of the code run only when the map is present - in other words is there a way I can check if it exists in the XAML? 我需要在名为我的地图控件的地图控件上以C#运行一些代码,但是如何使该部分代码仅在存在地图时才能运行-换句话说,有一种方法可以检查其中是否存在XAML?

Without the map any code referring to MyMap of course throws an error - is this the best way of going about this or am I missing a better way? 没有地图,任何引用MyMap的代码当然都会引发错误-这是解决此问题的最佳方法,还是我错过了更好的方法?

EDIT 编辑

I'm using an approach similar to the below to work out what template should be used depending on the MinWindowWidth. 我正在使用一种与以下类似的方法来确定应使用哪种模板,具体取决于MinWindowWidth。 So MyMap will only exist in the LargeTemplate etc 因此MyMap仅存在于LargeTemplate等中

<Page.Resources>
    <DataTemplate x:Key="SmallTemplate">
        <Grid>
            <Border Background="LightGray" Height="100" Width="100">
                <TextBlock Text="{Binding Text}" 
                          FontSize="48" Foreground="Green" />
            </Border>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="LargeTemplate">
        <Grid>
            <Border Background="LightGray" Height="200" Width="200">
                <TextBlock Text="{Binding Text}" 
                          FontSize="48" Foreground="Green" />
            </Border>
        </Grid>
    </DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="Small">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="MyGridView.ItemTemplate" Value="{StaticResource SmallTemplate}" />
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="Large">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="720" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="MyGridView.ItemTemplate" Value="{StaticResource LargeTemplate}" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups> 
    <GridView Name="MyGridView"/>
</Grid>

Add an x:Name to your VisualStateGroup eg. 将x:Name添加到您的VisualStateGroup中,例如。 AdaptiveVisualStateGroup , after that you can check for the CurrentState in your code like this: AdaptiveVisualStateGroup.CurrentState AdaptiveVisualStateGroup ,之后,您可以像下面这样检查代码中的CurrentState: AdaptiveVisualStateGroup.CurrentState

If the CurrentState equals Large or whatever the name is your VisualState where you have your map, you can run the code that references MyMap , otherwise you just skip it 如果CurrentState等于Large或您在其中拥有地图的VisualState的名称,则可以运行引用MyMap的代码,否则就跳过它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM