1

我有一个小SplitView应用程序,现在我将从ImageSource.page2mySplitpanel

如何访问所有参数,如SetSourceVisible在此处输入图像描述

这是我的代码

主页.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Width="938.137">
    <SplitView x:Name="MySplitView" DisplayMode="CompactOverlay"  IsPaneOpen="False" 
               CompactPaneLength="50" OpenPaneLength="150">
        <SplitView.Pane>
            <StackPanel Background="Gray">
                <Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="&#xE700;"
                    Width="50" Height="50" Background="Transparent" Click="HamburgerButton_Click"/>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="MenuButton1" FontFamily="Segoe MDL2 Assets" Content="&#xE825;"
                    Width="50" Height="50" Background="Transparent" Click="MenuButton1_Click"/>
                    <TextBlock Text="Button 1" FontSize="18" VerticalAlignment="Center" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="MenuButton2" FontFamily="Segoe MDL2 Assets" Content="&#xE10F;"
                        Width="50" Height="50" Background="Transparent" Click="MenuButton2_Click"/>
                    <TextBlock Text="Button 2" FontSize="18" VerticalAlignment="Center" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="MenuButton3" FontFamily="Segoe MDL2 Assets" Content="&#xE1D6;"
                        Width="50" Height="50" Background="Transparent" Click="MenuButton3_Click"/>
                    <TextBlock Text="Button 3" FontSize="18" VerticalAlignment="Center" />
                </StackPanel>
                     </StackPanel>
        </SplitView.Pane>
        <Frame x:Name="myFrame" />
    </SplitView>

</Page>

主页.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace App1
{

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            myFrame.Navigate(typeof(Page1));
        }

        private void MenuButton1_Click(object sender, RoutedEventArgs e)
        {

                myFrame.Navigate(typeof(Page2));
        }

        private void HamburgerButton_Click(object sender, RoutedEventArgs e)
        {
            MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen;
        }

        private void MenuButton2_Click(object sender, RoutedEventArgs e)
        {
            myFrame.Navigate(typeof(Page1)); //Home
        }

        private void MenuButton3_Click(object sender, RoutedEventArgs e)
        {
            //set image source at page2.myImagePage2
           // ??
        }
    }
}

Page2.xaml

<Page
    x:Class="App1.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="#FFE03535">
        <Image x:Name="myImagePage2" HorizontalAlignment="Left" Height="116" Margin="81,112,0,0" VerticalAlignment="Top" Width="175" Source="Assets/LockScreenLogo.png"/>

    </Grid>
</Page>

我想从MenuButton3_ClickImageSource更改at Page2.myImagePage2

如何访问里面的函数page2

该函数将Visibilty项目的 设置为Page2。我可以访问该功能并且项目变得不可见吗?

4

1 回答 1

1

您可以使用Page2( Page2.xaml.cs) 的代码隐藏来创建public将修改可见性的方法(如public void HideImage()public void ChangeImageSource())。

现在要在Click处理程序中获取当前页面,请使用以下Frame.Content属性:

private void MenuButton3_Click(object sender, RoutedEventArgs e)
{
   var page = myFrame.Content as Page2;
   if ( page != null )
   {
      //do something like calling your custom public method
      //page.HideImage();
   }
}
于 2018-01-14T07:09:39.020 回答