1

我的应用程序中的 QueryString 有问题。我的主页中有两个按钮,当用户单击一个按钮时,应用程序导航到第二页并将按钮 ID 存储在 Querystring 上。

现在,我遇到的问题是,如果用户单击按钮,则查询字符串“ID”键设置为相应的 ID 值,但该 ID 值将变为持久性。

IE

点击 Button1 => NavigationService("/Page2.xaml?ID=1",UriKind.Relative) => 检查 page2 的 onNavigated 事件中传入的 ID 值,您会发现 Querystring 在第二页中成功设置为 ID。

但是如果你点击后退按钮然后按button2

点击 Button2 => NavigationService("/Page2.xaml?ID=2",UriKind.Relative) => Check QueryString value in on Navidated event on page2,你发现ID键还是1的值。

如果我是通过按 button2 开始的,那么 ID 键的持久值将为 2。

我完全不知道会发生什么。我在页面之间进行了动画页面转换,但我不知道这会如何影响事情。有人知道会发生什么吗?如果我导航回主页,有没有办法重置整个应用程序?

在主页中

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            animation1.Stop();
            animation2.Stop();
            animation1.Begin();
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {          
            animation2.Begin();
            animation2.Completed += (x, y) => NavigationService.Navigate(new Uri("/fooApp;component/Page2.xaml?id=1", UriKind.Relative));                           
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            animation2.Begin();
            animation2.Completed += (x, y) => NavigationService.Navigate(new Uri("/fooApp;component/Page2.xaml?id=2", UriKind.Relative));          
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            animation2.Begin();
            animation2.Completed += (x, y) =>   NavigationService.Navigate(new Uri("/fooApp;component/Page2.xaml?id=3", UriKind.Relative));      
        }

在第 2 页

  protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e )
    {

       base.OnNavigatedTo(e);

       animation1.Stop();
       animation2.Stop();
       animation1.Begin();

    }

顺便说一句,我已经注释掉了整个 Page2 Page Loaded 事件。

更新

我注意到的一件事是程序甚至不运行第二个导航调用,它只运行第一个,我知道这是因为如果我将 button2 中的 Uri 源更改为空字符串并以 button1 为按钮运行程序即首先按下,无论我返回并按下哪个按钮,我都会使查询字符串值停留在 1。当我查看 page2 中 OnNavigatedTo 事件中的 NavigationEventArgs e 对象时,它的 URI 源将始终是第一个 uri 源。

我现在的猜测是,animation2.Completed 事件正在充当某种多播委托,无论首先放入其中的内容都会运行。此外,我删除了动画并且只有 Navigation 语句,程序工作很好,但没有动画。有人知道我如何清除动画2.完成事件吗?或者对替代实现有任何想法。

4

1 回答 1

1

基本上,您正在向Completed情节提要添加处理程序。处理程序导航到第Completed2 页。然后您返回并按下第二个按钮。由于您返回上一页,因此您正在重新使用相同的页面实例,因此也是相同的Storyboard实例。现在您正在向Completed您的Storyboard. 因此,当动画结束时,Storyboard有两个Completed处理程序,第一个首先执行,因此使用第一个参数导航到 Page2。

而不是每次都添加一个Completed处理程序,只做一次,并将 URL 存储在一个属性中:

在您的 XAML 中:

<Storyboard x:Key="Animation2" Completed="Storyboard_Completed">
    <!-- whatever-->
</Storyboard>    

然后在您的 C# 代码中:

protected string Uri { get; set; }

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    animation1.Stop();
    animation2.Stop();
    animation1.Begin();
}

private void button1_Click(object sender, RoutedEventArgs e)
{          
    this.Uri = "/fooApp;component/Page2.xaml?id=1";
    animation2.Begin();                       
}

private void button2_Click(object sender, RoutedEventArgs e)
{
    this.Uri = "/fooApp;component/Page2.xaml?id=2";
    animation2.Begin();          
}

private void button3_Click(object sender, RoutedEventArgs e)
{
    this.Uri = "/fooApp;component/Page2.xaml?id=3";
    animation2.Begin();              
}

private void Storyboard_Completed(object sender, EventArgs e)
{
    this.NavigationService.Navigate(new Uri(this.Uri, UriKind.Relative));      
}

奖金:

删除事件处理程序

如果要删除事件处理程序,只需使用-=运算符。但是你不能使用 lambda,你必须使用“经典”方法:

// Good
this.animation2.Completed -= Storyboard_Completed;

// Wrong
this.animation2.Completed -= (sender, e) => NavigationService.Navigate(new Uri("RandomText_JustWantCompletedEventToBeHit", UriKind.Relative));    

替代实施

如果您的所有按钮都是触发动画并重定向到 Page2,那么您可以Tag通过控件的属性以更通用的方式处理它。该Tag属性允许您存储要标识控件的任何对象。

XAML:

<Button Click="Button_Click" Content="Button 1" Tag="1" />
<Button Click="Button_Click" Content="Button 2" Tag="2" />
<Button Click="Button_Click" Content="Button 3" Tag="3" />

C#:

private void Button_Click(object sender, RoutedEventArgs e)
{
     var element = (FrameworkElement)sender;
     this.Url = "/fooApp;component/Page2.xaml?id=" + element.Tag.ToString();

     animation2.Begin();
}
于 2012-11-25T09:48:18.767 回答