C# 如何使用按钮触发适当的xaml页面

C# 如何使用按钮触发适当的xaml页面,c#,wpf,xml,xaml,C#,Wpf,Xml,Xaml,我使用的例子取自项目“japf\u fr\u slide\u动画”。我添加了触发相应页面的按钮,但被卡住了,不知道如何触发它们。以下是添加了按钮的引用项目的所有页面。有人能帮助我如何根据按钮触发页面吗 view1.xaml <UserControl x:Class="FirstTry.view1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas

我使用的例子取自项目“japf\u fr\u slide\u动画”。我添加了触发相应页面的按钮,但被卡住了,不知道如何触发它们。以下是添加了按钮的引用项目的所有页面。有人能帮助我如何根据按钮触发页面吗

view1.xaml

<UserControl x:Class="FirstTry.view1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid Background="LightBlue">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view1" />
        <Button Name="show2" Content="show second xaml" VerticalAlignment="Center" Width="150"></Button>
    </Grid>
</UserControl>
<UserControl x:Class="FirstTry.view1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FirstTry"
    Height="300" Width="300">
    <Grid Background="LightBlue">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view1" />

        <!-- Button fires command with the value 1 as next pageindex --> 
        <Button Name="show2" Content="show second xaml" VerticalAlignment="Center" Width="150"
                Command="{x:Static local:Commands.SlideToPage}" CommandParameter="1"></Button>
    </Grid>
</UserControl>

view2.xaml

<UserControl x:Class="FirstTry.view2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid Background="LightCoral">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view2"/>
        <Button Name="show3" Content="show third xaml" VerticalAlignment="Center" Width="150"></Button>
    </Grid>
</UserControl>
<UserControl x:Class="FirstTry.view2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FirstTry"
    Height="300" Width="300">
    <Grid Background="LightCoral">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view2"/>

        <!-- Button fires command with the value 2 as next pageindex -->
        <Button Name="show3" Content="show third xaml" VerticalAlignment="Center" Width="150"
                Command="{x:Static local:Commands.SlideToPage}" CommandParameter="2 </Button>
    </Grid>
</UserControl>

view3.xaml

<UserControl x:Class="FirstTry.view3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid Background="LightGreen">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view3"/>
        <Button Name="show1" Content="show first xaml" VerticalAlignment="Center" Width="150"></Button>
    </Grid>
</UserControl>
<UserControl x:Class="FirstTry.view3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FirstTry"
    Height="300" Width="300">
    <Grid Background="LightGreen">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view3"/>

        <!-- Button fires command with the value 0 as next pageindex -->
        <Button Name="show1" Content="show first xaml" VerticalAlignment="Center" Width="150"
                Command="{x:Static local:Commands.SlideToPage}" CommandParameter="0"></Button>
    </Grid>
</UserControl>

Window1.xaml

<Window x:Class="FirstTry.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" SizeToContent="WidthAndHeight" ResizeMode="NoResize">

    <Window.Resources>
        <XmlDataProvider x:Key="views">
            <x:XData>
                <Views xmlns="">
                    <View Title="View1">
                        <Page Source="view1.xaml"/> 
                    </View>
                    <View Title="View2">
                        <Page Source="view2.xaml"/>
                    </View>
                    <View Title="View3">
                        <Page Source="view3.xaml"/>
                    </View>
                </Views>
            </x:XData>
        </XmlDataProvider>

        <Storyboard x:Key="slideLeftToRight"  
                    TargetProperty="RenderTransform.(TranslateTransform.X)"
                    AccelerationRatio=".4"
                    DecelerationRatio=".4">
            <DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="300" To="0"/>
            <DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="-300"/>
        </Storyboard>

        <Storyboard x:Key="slideRightToLeft" 
                    TargetProperty="RenderTransform.(TranslateTransform.X)"
                    AccelerationRatio=".4"
                    DecelerationRatio=".4">
            <DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="-300" To="0"/>
            <DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="300"/>
        </Storyboard>

        <VisualBrush x:Key="VisualBrush1" Visual="{Binding ElementName=viewer}"/>

    </Window.Resources>

    <Grid>
        <StackPanel>

            <StackPanel Orientation="Horizontal">
                <ListBox x:Name="viewList" Height="20" Width="300" SelectedIndex="0"
                    ItemsSource="{Binding Source={StaticResource views}, XPath=Views/View}"
                    DisplayMemberPath="@Title"                    
                    SelectionChanged="viewList_SelectionChanged">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
            </StackPanel>

            <Grid Width="300" Height="300">



                <Border x:Name="bordervisual" Width="300">
                    <Rectangle x:Name="rectanglevisual"/>
                    <Border.RenderTransform>
                        <TranslateTransform/>
                    </Border.RenderTransform>
                </Border>

                <ItemsControl x:Name="viewer" DataContext="{Binding Path=SelectedItem, ElementName=viewList}"
                    ItemsSource="{Binding XPath=Page}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Frame x:Name="frame" Source="{Binding XPath=@Source}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.RenderTransform>
                        <TranslateTransform/>
                    </ItemsControl.RenderTransform>
                </ItemsControl>
            </Grid>
        </StackPanel>       
    </Grid>
</Window>
<Window x:Class="FirstTry.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:FirstTry"
        Title="MainWindow" SizeToContent="WidthAndHeight" ResizeMode="NoResize">
    <Window.CommandBindings>
        <!-- Make the window listen for the SlideToPage command -->
        <CommandBinding Command="{x:Static local:Commands.SlideToPage}"
                        CanExecute="SlideToPage_CanExecute"
                        Executed="SlideToPage_Executed" />
    </Window.CommandBindings>

    <Window.Resources>
        <XmlDataProvider x:Key="views">
            <x:XData>
                <Views xmlns="">
                    <View Title="View1">
                        <Page Source="view1.xaml"/> 
                    </View>
                    <View Title="View2">
                        <Page Source="view2.xaml"/>
                    </View>
                    <View Title="View3">
                        <Page Source="view3.xaml"/>
                    </View>
                </Views>
            </x:XData>
        </XmlDataProvider>

        <Storyboard x:Key="slideLeftToRight"  
                    TargetProperty="RenderTransform.(TranslateTransform.X)"
                    AccelerationRatio=".4"
                    DecelerationRatio=".4">
            <DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="300" To="0"/>
            <DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="-300"/>
        </Storyboard>

        <Storyboard x:Key="slideRightToLeft" 
                    TargetProperty="RenderTransform.(TranslateTransform.X)"
                    AccelerationRatio=".4"
                    DecelerationRatio=".4">
            <DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="-300" To="0"/>
            <DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="300"/>
        </Storyboard>

        <VisualBrush x:Key="VisualBrush1" Visual="{Binding ElementName=viewer}"/>
    </Window.Resources>

    <Grid>
        <StackPanel>

            <StackPanel Orientation="Horizontal">
                <ListBox x:Name="viewList" Height="20" Width="300" SelectedIndex="0"
                    ItemsSource="{Binding Source={StaticResource views}, XPath=Views/View}"
                    DisplayMemberPath="@Title"                    
                    SelectionChanged="viewList_SelectionChanged">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
            </StackPanel>

            <Grid Width="300" Height="300">
                <Border x:Name="bordervisual" Width="300">
                    <Rectangle x:Name="rectanglevisual"/>
                    <Border.RenderTransform>
                        <TranslateTransform/>
                    </Border.RenderTransform>
                </Border>

                <ItemsControl x:Name="viewer" DataContext="{Binding Path=SelectedItem, ElementName=viewList}"
                    ItemsSource="{Binding XPath=Page}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Frame x:Name="frame" Source="{Binding XPath=@Source}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.RenderTransform>
                        <TranslateTransform/>
                    </ItemsControl.RenderTransform>
                </ItemsControl>
            </Grid>
        </StackPanel>       
    </Grid>
</Window>

Window1.xaml.cs

public partial class Window1 : Window
    {
        private static int oldSelectedIndex = 0;

        public Window1()
        {
            InitializeComponent();
        }

        public RenderTargetBitmap RenderBitmap(FrameworkElement element)
        {
            double topLeft = 0;
            double topRight = 0;
            int width = (int)element.ActualWidth;
            int height = (int)element.ActualHeight;
            double dpiX = 96; // this is the magic number
            double dpiY = 96; // this is the magic number

            PixelFormat pixelFormat = PixelFormats.Default;
            VisualBrush elementBrush = new VisualBrush(element);
            DrawingVisual visual = new DrawingVisual();
            DrawingContext dc = visual.RenderOpen();

            dc.DrawRectangle(elementBrush, null, new Rect(topLeft, topRight, width, height));
            dc.Close();

            RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, dpiX, dpiY, pixelFormat);

            bitmap.Render(visual);
            return bitmap;
        } 

        private void viewList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            XmlElement root = (XmlElement)viewer.DataContext;
            XmlNodeList xnl = root.SelectNodes("Page");

            if (viewer.ActualHeight > 0 && viewer.ActualWidth > 0)
            {
                RenderTargetBitmap rtb = RenderBitmap(viewer);
                rectanglevisual.Fill = new ImageBrush(BitmapFrame.Create(rtb));
            }

            viewer.ItemsSource = xnl;

            if (oldSelectedIndex < viewList.SelectedIndex)
            {
                viewer.BeginStoryboard((Storyboard)this.Resources["slideLeftToRight"]);
            }
            else
            {
                viewer.BeginStoryboard((Storyboard)this.Resources["slideRightToLeft"]);
            }

            oldSelectedIndex = viewList.SelectedIndex;
        }
    }
public partial class Window1 : Window
{
    private static int oldSelectedIndex = 0;

    public Window1()
    {
        InitializeComponent();
    }

    public RenderTargetBitmap RenderBitmap(FrameworkElement element)
    {
        double topLeft = 0;
        double topRight = 0;
        int width = (int)element.ActualWidth;
        int height = (int)element.ActualHeight;
        double dpiX = 96; // this is the magic number
        double dpiY = 96; // this is the magic number

        PixelFormat pixelFormat = PixelFormats.Default;
        VisualBrush elementBrush = new VisualBrush(element);
        DrawingVisual visual = new DrawingVisual();
        DrawingContext dc = visual.RenderOpen();

        dc.DrawRectangle(elementBrush, null, new Rect(topLeft, topRight, width, height));
        dc.Close();

        RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, dpiX, dpiY, pixelFormat);

        bitmap.Render(visual);
        return bitmap;
    }

    private void viewList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        XmlElement root = (XmlElement)viewer.DataContext;
        XmlNodeList xnl = root.SelectNodes("Page");

        if (viewer.ActualHeight > 0 && viewer.ActualWidth > 0)
        {
            RenderTargetBitmap rtb = RenderBitmap(viewer);
            rectanglevisual.Fill = new ImageBrush(BitmapFrame.Create(rtb));
        }

        viewer.ItemsSource = xnl;

        if (oldSelectedIndex < viewList.SelectedIndex)
        {
            viewer.BeginStoryboard((Storyboard)this.Resources["slideLeftToRight"]);
        }
        else
        {
            viewer.BeginStoryboard((Storyboard)this.Resources["slideRightToLeft"]);
        }

        oldSelectedIndex = viewList.SelectedIndex;
    }

    private void SlideToPage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        //The command can always be executed
        e.CanExecute = true;
    }

    private void SlideToPage_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        //When the command is executed, we find the new pagenumber passed as a parameter
        //and switch the selected item of the ListBox, so the animation will run.
        int pagenumber = 0;

        if (int.TryParse(e.Parameter.ToString(), out pagenumber))
        {
            viewList.SelectedIndex = pagenumber;
        }
    } 
}
公共部分类窗口1:窗口
{
私有静态int oldSelectedIndex=0;
公共窗口1()
{
初始化组件();
}
公共RenderTargetBitmap RenderBitmap(FrameworkElement元素)
{
双左上角=0;
双右上角=0;
int width=(int)element.ActualWidth;
int height=(int)element.ActualHeight;
double dpiX=96;//这是一个神奇的数字
double dpiY=96;//这是幻数
PixelFormat PixelFormat=PixelFormats.Default;
VisualBrush元素Brush=新的VisualBrush(元素);
DrawingVisual=新建DrawingVisual();
DrawingContext dc=visual.renderropen();
DrawRectangle(elementBrush,null,新矩形(左上、右上、宽度、高度));
dc.Close();
RenderTargetBitmap位图=新的RenderTargetBitmap(宽度、高度、dpiX、dpiY、像素格式);
位图渲染(可视化);
返回位图;
} 
private void viewList_SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
XmlElement根=(XmlElement)viewer.DataContext;
XmlNodeList xnl=root.SelectNodes(“页面”);
如果(viewer.ActualHeight>0&&viewer.ActualWidth>0)
{
RenderTargetBitmap rtb=RenderBitmap(查看器);
rectanglevisual.Fill=新的ImageBrush(BitmapFrame.Create(rtb));
}
viewer.ItemsSource=xnl;
如果(旧的SelectedIndex
一个非常简单的方法是,每当单击按钮时,从每个按钮发出一个命令。应选择的新页码可以通过向命令传递参数发送给命令

在窗口中,我们可以创建命令的绑定,这样当单击按钮时它会收到通知,然后我们可以在列表框中选择新页面,从而使动画运行

我已经编写了一些代码来演示这一点,并且只是在您已有的基础上添加了一点:

view1.xaml

<UserControl x:Class="FirstTry.view1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid Background="LightBlue">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view1" />
        <Button Name="show2" Content="show second xaml" VerticalAlignment="Center" Width="150"></Button>
    </Grid>
</UserControl>
<UserControl x:Class="FirstTry.view1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FirstTry"
    Height="300" Width="300">
    <Grid Background="LightBlue">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="view1" />

        <!-- Button fires command with the value 1 as next pageindex --> 
        <Button Name="show2" Content="show second xaml" VerticalAlignment="Center" Width="150"
                Command="{x:Static local:Commands.SlideToPage}" CommandParameter="1"></Button>
    </Grid>
</UserControl>

非常感谢您帮助我解决这个问题。单击按钮时高亮显示listview如何,就像选择viewList\u SelectionChanged时高亮显示一样?没问题。我很高兴它解决了你的问题。如果在“viewList.SelectedIndex=pagenumber;”之后调用“viewList.Focus();”,它将选择列表框并高亮显示它。我想那可能是用的。太好了!非常感谢你。