怎样仅通过xaml实现页面淡入淡出切换

luckywll 2013-08-13 09:54:00
wpf中实现点击几个按钮,主窗口区域淡入淡出切换页面,我用的方法需要在cs里面写代码,想研究一下怎么在xaml里面就实现,最好同时按钮的click相应在cs里面还能响应,处理一些后台的东西?
...全文
290 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
luckywll 2013-08-15
  • 打赏
  • 举报
回复
能来个管理吧把打错的照片删除啊
luckywll 2013-08-15
  • 打赏
  • 举报
回复


<StackPanel Grid.Row="1" Orientation="Horizontal">
<RadioButton Content="page1" Checked="Button_Click_1" GroupName="r1" Style="{StaticResource myRadio}" x:Name="radioDef" Margin="5" Width="70" Height="90">
<RadioButton.Background>
<ImageBrush ImageSource="res/1.png"/>
</RadioButton.Background>
</RadioButton>
<RadioButton Content="page2" Checked="Button_Click_2" GroupName="r1" Style="{StaticResource myRadio}" Margin="5" Width="70" Height="90" >
<RadioButton.Background>
<ImageBrush ImageSource="res/2.png"/>
</RadioButton.Background>
</RadioButton>
</StackPanel>
<Frame Grid.Row="2" x:Name="page2frame" NavigationUIVisibility="Hidden" Navigating="NavigationWindow_Navigating"></Frame>


public MainWindow()
{
InitializeComponent();
radioDef.IsChecked = true;
}

//淡入淡出切换
private bool _allowDirectNavigation = false;
private NavigatingCancelEventArgs _navArgs = null;
Page1 page1 = new Page1();
Page2 page2 = new Page2();
int nPage;

private void NavigationWindow_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (Content != null && !_allowDirectNavigation)
{
e.Cancel = true;
_navArgs = e;
this.IsHitTestVisible = false;
DoubleAnimation da = new DoubleAnimation(1, 0.5, new Duration(TimeSpan.FromMilliseconds(200)));
Storyboard.SetTarget(da, page2frame);
Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.Opacity)"));
Storyboard bord = new Storyboard();
bord.Children.Add(da);
bord.Completed += FadeOutCompleted;
bord.Begin();
//da.Completed += FadeOutCompleted;
//this.BeginAnimation(OpacityProperty, da);
}
_allowDirectNavigation = false;
}

private void FadeOutCompleted(object sender, EventArgs e)
{
//(sender as AnimationClock).Completed -= FadeOutCompleted;
//(sender as Storyboard).Completed -= FadeOutCompleted;

this.IsHitTestVisible = true;

_allowDirectNavigation = true;
switch (_navArgs.NavigationMode)
{
case NavigationMode.New:
if (_navArgs.Uri == null)
{
//NavigationService.Navigate(_navArgs.Content);
page2frame.Navigate(_navArgs.Content);
}
else
{
//NavigationService.Navigate(_navArgs.Uri);
page2frame.Navigate(_navArgs.Uri);
page2frame.RemoveBackEntry();
}
break;
case NavigationMode.Back:
//NavigationService.GoBack();
page2frame.GoBack();
break;

case NavigationMode.Forward:
//NavigationService.GoForward();
page2frame.GoForward();
break;
case NavigationMode.Refresh:
//NavigationService.Refresh();
page2frame.Refresh();
break;
}

Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
(ThreadStart)delegate()
{
DoubleAnimation da = new DoubleAnimation(0.5,1, new Duration(TimeSpan.FromMilliseconds(200)));
Storyboard.SetTarget(da, page2frame);
Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.Opacity)"));
Storyboard bord = new Storyboard();
bord.Children.Add(da);
bord.Begin();
//this.BeginAnimation(OpacityProperty, da);
});
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (nPage != 1)
{
page2frame.Navigate(page1);
nPage = 1;
}
}


private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (nPage != 2)
{
page2frame.Navigate(page2);
nPage = 2;
}
}


不是一个界面加载,而是两个page切换,我知道界面的事件处理可以卸载xaml的triger里面,但是这里用到两个托管,不知道怎么写了
Bonjour-你好 2013-08-14
  • 打赏
  • 举报
回复
不知是不是这样

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation Duration="0:0:3" Storyboard.TargetProperty="Opacity" From="0" To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>

<Grid Background="Khaki"/>
</Window>


luckywll 2013-08-14
  • 打赏
  • 举报
回复
xaml里面 Navigating="NavigationWindow_Navigating"

private void NavigationWindow_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (Content != null && !_allowDirectNavigation)
            {
                e.Cancel = true;
                _navArgs = e;
                this.IsHitTestVisible = false;
                DoubleAnimation da = new DoubleAnimation(0.3d, new Duration(TimeSpan.FromMilliseconds(300)));
                da.Completed += FadeOutCompleted;
                this.BeginAnimation(OpacityProperty, da);
            }
            _allowDirectNavigation = false;
        }
private void FadeOutCompleted(object sender, EventArgs e)
        {
            (sender as AnimationClock).Completed -= FadeOutCompleted;

            this.IsHitTestVisible = true;

            _allowDirectNavigation = true;
            switch (_navArgs.NavigationMode)
            {
                case NavigationMode.New:
                    if (_navArgs.Uri == null)
                    {
                        NavigationService.Navigate(_navArgs.Content);
                    }
                    else
                    {
                        NavigationService.Navigate(_navArgs.Uri);
                    }
                    break;
                case NavigationMode.Back:
                    NavigationService.GoBack();
                    break;

                case NavigationMode.Forward:
                    NavigationService.GoForward();
                    break;
                case NavigationMode.Refresh:
                    NavigationService.Refresh();
                    break;
            }

            Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
                (ThreadStart)delegate()
            {
                DoubleAnimation da = new DoubleAnimation(1.0d, new Duration(TimeSpan.FromMilliseconds(200)));
                this.BeginAnimation(OpacityProperty, da);
            });
        }
就是想完全有xaml里面用triger实现
Bonjour-你好 2013-08-14
  • 打赏
  • 举报
回复
那你贴一下你的cs中的代码吧,看你描述,感觉是动画

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧