zmdoyouknow 2017-07-08 11:11 采纳率: 33.3%
浏览 1056

WPF RelayCommand命令打开此页面frame跳转

Page页面

     <Grid>
        <StackPanel Orientation="Vertical" Margin="39,10,486,193" Width="154" Height="235">
            <Button Name="Image" Width="154" Height="198"  Style="{StaticResource Image}"/>
            <Button Name="Button" Click="Button_Click" Template="{StaticResource Start}" Width="154" Height="38"  Content="start" Background="#FF26CBC1" FontFamily="Microsoft YaHei" FontSize="16" FontWeight="ExtraBlack"></Button>
        </StackPanel>

        <Frame Name="Frame" NavigationUIVisibility="Hidden"></Frame>
    </Grid>

Image

 <Style x:Key="Image" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Width="154" Height="198">
                        <Image Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Source="/BanlieueCraft-Lanucher;component/Resources/mc.png" Stretch="Fill"></Image>
                        <Polygon Name="po1" Width="15" Height="15" Points="0, 0 15, 15 15, 0" Fill="Aqua" Margin="139,0,0,183"/>
                        <Label Width="154" Height="30" Content="starts" FontSize="14" FontWeight="Bold" Margin="0,170,0,0" Foreground="White" HorizontalContentAlignment="Center"></Label>
                        <Grid Name="grid" Visibility="Hidden">
                            <Button Template="{DynamicResource Sjx}"></Button>
                       </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property ="IsMouseOver" Value="True">
                            <Setter Property="Visibility" Value="Hidden" TargetName="po1"></Setter>
                            <Setter Property="Visibility" Value="Visible" TargetName="grid"></Setter>
                            <!--<Setter Property="Visibility" Value="Visible" TargetName="gg"></Setter>-->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Sjx

 <ControlTemplate x:Key="Sjx" TargetType="Button">
        <Grid>
            <Grid Margin="114,0,0,158">
                <Polygon Width="40" Height="40" Points="0, 0 40, 40 40, 0" Fill="Aqua"/>
                <TextBlock Foreground="#FF094E4D" Text="..." TextAlignment="Justify" Margin="20,-5,0,0" FontWeight="Bold" FontSize="18"/>
            </Grid>
            <Grid Name="grid" Visibility="Hidden" Width="154" Height="198" Background="#FF39405A" Opacity="0.9">
                <Button Command="{Binding Jiedian}" Content="节点" Width="40" Height="40" Margin="20,10,84,128" Template="{StaticResource Tubiao5}"></Button>
                <Button Command="{Binding Vers}" Content="版本" Width="40" Height="40" Margin="60,10,0,128" Template="{StaticResource Tubiao6}"></Button>
                <Button Command="{Binding Way}" Content="路径" Width="40" Height="40" Margin="20,30,84,30" Template="{StaticResource Tubiao1}"></Button>
                <Button Command="{Binding Java}" Content="JAVA" Width="40" Height="40" Margin="60,30,0,30" Template="{StaticResource Tubiao2}"></Button>
                <Button Command="{Binding Memory}" Content="内存" Width="40" Height="40" Margin="20,150,84,0" Template="{StaticResource Tubiao3}"></Button>
                <Button Command="{Binding Size}" Content="分辨率" Width="40" Height="40" Margin="60,150,0,0" Template="{StaticResource Tubiao4}"></Button>
            </Grid>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property ="IsMouseOver" Value="True">
                <Setter Property="Visibility" Value="Visible" TargetName="grid"></Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

UserViewModel

  public class UserViewModel: BaseNotifyPropertyChanged
    {
        public static RelayCommand Way { get; private set; }
        public static RelayCommand Java { get; private set; }
        public static RelayCommand Memory { get; private set; }
        public static RelayCommand Size { get; private set; }
        public static RelayCommand Vers { get; private set; }
        //public static readonly CommandBinding VersBinding;
        static UserViewModel()
        {
            Way = new RelayCommand(ShowWay);
            Java = new RelayCommand(ShowJava);
            Memory = new RelayCommand(ShowMemory);
            Size = new RelayCommand(ShowSize);
            Vers = new RelayCommand(ShowVers);
            //VersBinding = new CommandBinding(Vers);
           // VersBinding.Executed += ShowVers();
        }

        public static void ShowWay()
        {
            var gamepath = new Page.ViewPage.GameWay();
            gamepath.ShowDialog();
        }
        public static void ShowJava()
        {
            var gamejava = new Page.ViewPage.GameJava();
            gamejava.ShowDialog();
        }
        public static void ShowMemory()
        {
            var gamememory = new Page.ViewPage.GameMemory();
            gamememory.ShowDialog();
        }
        public static void ShowSize()
        {
            var gamesize = new Page.ViewPage.GameSize();
            gamesize.ShowDialog();            
        }
       ** private static void ShowVers()
        {
            MessageBox.Show("1");

            //var t = e.Parameter as FrameworkElement;
            //var a = sender as Button;
            //var b = GetParentObject<Grid>(a,"");
            //var gameversion = new Page.Game();
            //gameversion.Frame.Navigate(new Uri("/Page/GameVersion.xaml", UriKind.Relative));           

        }**
        public static T GetParentObject<T>(DependencyObject obj, string name) where T : FrameworkElement
        {
            DependencyObject parent = VisualTreeHelper.GetParent(obj);

            while (parent != null)
            {
                if (parent is T && (((T)parent).Name == name | string.IsNullOrEmpty(name)))
                {
                    return (T)parent;
                }

                parent = VisualTreeHelper.GetParent(parent);
            }

            return null;
        }
    }

Image的Style里有按钮A,按钮bingding了个命令Vers
想实现A点击执行命令Vers,页面Frame.Navigate.....跳转。

尝试了实例化这个Page页面,不行。查找控件执行也不行。

  • 写回答

3条回答 默认 最新

  • 风平-浪静 2017-07-09 01:50
    关注

    NavigationService nav = NavigationService.GetNavigationService(this);
    nav.Navigate(page);
    使用这个试试,看看可以不。

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况