Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C WPF-使用App.content转换ContentControl_C#_Wpf_Animation_User Controls_Mahapps.metro - Fatal编程技术网

C# C WPF-使用App.content转换ContentControl

C# C WPF-使用App.content转换ContentControl,c#,wpf,animation,user-controls,mahapps.metro,C#,Wpf,Animation,User Controls,Mahapps.metro,我以MahApps Metro Dark为主题,我看了这个主题的动画 我走到了一条死胡同:事实上,我创建了一个在不同用户控件之间切换的系统,也就是说,我只有一个窗口,点击不同的按钮,我就有这个或那个样的用户控件。但是现在我使用这个系统开关,我没有动画,只有应用程序的开始 如何为UserControl主题中的每次更改制作动画 有人问我:使用TransitionContentControl 但我的切换器是这样做的: class Switcher { public static UserCon

我以MahApps Metro Dark为主题,我看了这个主题的动画

我走到了一条死胡同:事实上,我创建了一个在不同用户控件之间切换的系统,也就是说,我只有一个窗口,点击不同的按钮,我就有这个或那个样的用户控件。但是现在我使用这个系统开关,我没有动画,只有应用程序的开始

如何为UserControl主题中的每次更改制作动画

有人问我:使用TransitionContentControl

但我的切换器是这样做的:

class Switcher
{
    public static UserControl WClient;
    public static UserControl WHome;
    public static UserControl WDataBase;

    public Switcher()
    {
        WClient = new Windows.Client();
        WHome = new Windows.Home();
        WDataBase = new Windows.DataBase();
    }

    public static void currentWindow(UserControl window, string color)
    {

        Window curApp = Application.Current.MainWindow;
        curApp.Content = window;

        if (window == WClient)
        {
            curApp.Title = "CLIENT - INFO-TOOLS - BY NAOGRAFIX";
        }
        else if (window == WDataBase)
        {
            curApp.Title = "DATABASE - INFO-TOOLS - BY NAOGRAFIX";
        }
        else
        {
            curApp.Title = "HOME - INFO-TOOLS - BY NAOGRAFIX";
        }

        currentColor(color); 
  }
}

现在,当我点击一个按钮来切换userControl时,我使用以下命令:

private void BtnDataBase_Click(object sender, RoutedEventArgs e)
{
     var color = "Red";

     if (DataBase.isConnected) { color = "Green"; }
     Switcher.currentWindow(Switcher.WDataBase, color);
}
我使用内容,我不知道我是否可以使用TransitioningContentControl

帮助:


Nao*

正如您所说,您确实需要使用转换内容控制。您可以将其添加为窗口的直接内容,然后从主窗口按名称访问它并更改其内容

Xaml

您将需要xml名称空间定义

xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"

您可以使用TransitioningContentControl上的transition属性更改转换,下面的WPF XAML显示了MahApps.Metro TransitioningContentControl的使用

单击内容列表框以切换内容

在转换列表框中选择转换效果,然后更改选定内容以查看效果

<Window x:Class="WpfMahApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800">
    <Window.Resources>
        <TextBlock x:Key="Content1" Width="400" Height="200" Text="Content 1: TextBox" Background="Aqua" />
        <Canvas x:Key="Content2" Width="200" Height="400" Background="DarkOrange">
            <Ellipse Fill="YellowGreen" Stroke="Black"  Width="100" Height="200" />
            <Label Content="Content2: Canvas" />
        </Canvas>
        <Border x:Key="Content3" Width="100" Height="100" Background="Yellow" BorderBrush="Blue" BorderThickness="2" CornerRadius="4">
            <TextBlock Text="Content3: Border" />
        </Border>
    </Window.Resources>
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Vertical" >
            <CheckBox Margin="4" Content="Is Transitioning" IsChecked="{Binding ElementName=TransitioningContentControl,Path=IsTransitioning , Mode=OneWay}" />
            <StackPanel Orientation="Vertical" Margin="8">
                <TextBlock Text="Content" FontWeight="Bold"/>
                <ListBox Name="ContentSelection" HorizontalAlignment="Left">
                    <ListBoxItem Content="Content 1" Tag="{StaticResource Content1}" />
                    <ListBoxItem Content="Content 2" Tag="{StaticResource Content2}" />
                    <ListBoxItem Content="Content 3" Tag="{StaticResource Content3}" />
                </ListBox>
            </StackPanel>
            <StackPanel Orientation="Vertical" Margin="8">
                <TextBlock Text="Transition" FontWeight="Bold" />
                <ListBox Name="Transition" HorizontalAlignment="Left">
                    <ListBoxItem Content="Default" Tag="{x:Static mah:TransitionType.Default}"/>
                    <ListBoxItem Content="Normal" Tag="{x:Static mah:TransitionType.Normal}"/>
                    <ListBoxItem Content="Up" Tag="{x:Static mah:TransitionType.Up}"/>
                    <ListBoxItem Content="Down" Tag="{x:Static mah:TransitionType.Down}"/>
                    <ListBoxItem Content="Left" Tag="{x:Static mah:TransitionType.Left}" />
                    <ListBoxItem Content="Right" Tag="{x:Static mah:TransitionType.Right}"/>
                    <ListBoxItem Content="LeftReplace" Tag="{x:Static mah:TransitionType.LeftReplace}"/>
                    <ListBoxItem Content="RightReplace" Tag="{x:Static mah:TransitionType.RightReplace}"/>
                </ListBox>
            </StackPanel>
        </StackPanel>
        <mah:TransitioningContentControl Margin="8" 
            Name="TransitioningContentControl"
            Background="Beige" BorderBrush="Black" BorderThickness="1"
            Content="{Binding ElementName=ContentSelection, Path=SelectedValue.Tag}" 
            Transition="{Binding ElementName=Transition, Path=SelectedValue.Tag}" />
    </StackPanel>
</Window>

@djack109:当您添加>新建项>Visual C>WPF>窗口WPF时,Visual Studio会自动为您生成cs代码。然后可以从答案中粘贴XAML代码。您还需要添加对nuget包MahApps.Metro的引用。哦,对了,您的代码中没有任何c?啊,是的,我能看到发生了什么:
xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
<Window x:Class="WpfMahApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800">
    <Window.Resources>
        <TextBlock x:Key="Content1" Width="400" Height="200" Text="Content 1: TextBox" Background="Aqua" />
        <Canvas x:Key="Content2" Width="200" Height="400" Background="DarkOrange">
            <Ellipse Fill="YellowGreen" Stroke="Black"  Width="100" Height="200" />
            <Label Content="Content2: Canvas" />
        </Canvas>
        <Border x:Key="Content3" Width="100" Height="100" Background="Yellow" BorderBrush="Blue" BorderThickness="2" CornerRadius="4">
            <TextBlock Text="Content3: Border" />
        </Border>
    </Window.Resources>
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Vertical" >
            <CheckBox Margin="4" Content="Is Transitioning" IsChecked="{Binding ElementName=TransitioningContentControl,Path=IsTransitioning , Mode=OneWay}" />
            <StackPanel Orientation="Vertical" Margin="8">
                <TextBlock Text="Content" FontWeight="Bold"/>
                <ListBox Name="ContentSelection" HorizontalAlignment="Left">
                    <ListBoxItem Content="Content 1" Tag="{StaticResource Content1}" />
                    <ListBoxItem Content="Content 2" Tag="{StaticResource Content2}" />
                    <ListBoxItem Content="Content 3" Tag="{StaticResource Content3}" />
                </ListBox>
            </StackPanel>
            <StackPanel Orientation="Vertical" Margin="8">
                <TextBlock Text="Transition" FontWeight="Bold" />
                <ListBox Name="Transition" HorizontalAlignment="Left">
                    <ListBoxItem Content="Default" Tag="{x:Static mah:TransitionType.Default}"/>
                    <ListBoxItem Content="Normal" Tag="{x:Static mah:TransitionType.Normal}"/>
                    <ListBoxItem Content="Up" Tag="{x:Static mah:TransitionType.Up}"/>
                    <ListBoxItem Content="Down" Tag="{x:Static mah:TransitionType.Down}"/>
                    <ListBoxItem Content="Left" Tag="{x:Static mah:TransitionType.Left}" />
                    <ListBoxItem Content="Right" Tag="{x:Static mah:TransitionType.Right}"/>
                    <ListBoxItem Content="LeftReplace" Tag="{x:Static mah:TransitionType.LeftReplace}"/>
                    <ListBoxItem Content="RightReplace" Tag="{x:Static mah:TransitionType.RightReplace}"/>
                </ListBox>
            </StackPanel>
        </StackPanel>
        <mah:TransitioningContentControl Margin="8" 
            Name="TransitioningContentControl"
            Background="Beige" BorderBrush="Black" BorderThickness="1"
            Content="{Binding ElementName=ContentSelection, Path=SelectedValue.Tag}" 
            Transition="{Binding ElementName=Transition, Path=SelectedValue.Tag}" />
    </StackPanel>
</Window>