我收到错误:在名称范围内找不到名称 - 在MVVMLight应用程序中从一个页面移动到另一个页面时

问题描述 投票:0回答:1

我有一个MVVMLight多页面应用程序有两个页面,我可以通过单击相应的按钮从一个页面导航到另一个页面。

在第二页中,我有一个加载器动画,每次在textBox字段中输入内容时都会触发。一切都很好;从第一页开始,我可以转到第二页,然后在textBox中键入内容并开始动画。问题是,如果我转到第二页,然后我回到第一页,然后我再次转到第二页并在textBox中键入内容我收到一条错误消息,指出加载程序的名称不是存在,有趣的是,在我离开页面并回来之前,我没有收到此错误。

知道为什么动画在离开页面并回来后停止工作吗?

编辑:这是一个完整项目的链接。 https://www.dropbox.com/sh/yf87shw5rzxtxen/AAClTesIGpLKl6IzV-6pjfEfa?dl=0

要复制错误,请执行以下操作...

  1. 下载并打开应用程序。
  2. 转到第2页。
  3. 在textBox中键入内容(动画应该开始)。
  4. 回到第1页,什么都不做。
  5. 再次转到第2页并尝试在textBox中输入内容(您应该在此处看到错误)。

错误:

附加信息:在'TwoViews.Views.SecondView'的名称范围内找不到'rectangleLoader'名称。

enter image description here

XAML

<UserControl x:Class="TwoViews.Views.SecondView"
             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"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Grid>
        <Rectangle x:Name="rectangleLoader" Fill="#FFB2B2FF" HorizontalAlignment="Left" Height="19" Margin="26,89,0,0" VerticalAlignment="Top" Width="248"/>
        <TextBox x:Name="textBoxFileName" 
                     HorizontalAlignment="Left" 
                     Height="35" Width="180" 
                     Margin="26,125,0,0" 
                     VerticalAlignment="Top" 
                     Text="{Binding InputFileNameChanged, UpdateSourceTrigger=PropertyChanged}" FontSize="18"/>
    </Grid>
</UserControl>

SecondView.xaml.cs

namespace TwoViews.Views
{
    public partial class SecondView : UserControl
    {
        private Storyboard loaderStoryboard;

        public SecondView()
        {
            InitializeComponent();

            Messenger.Default.Register<MessageSearchStatus>(this, messageSearchStatus => ReceivedSearchStatus(messageSearchStatus));

            /// Animation for loader
            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = 100;
            myDoubleAnimation.To = 0;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(.1));
            myDoubleAnimation.AutoReverse = true;
            myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

            loaderStoryboard = new Storyboard();
            loaderStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, rectangleLoader.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));
        }

        private void ReceivedSearchStatus(MessageSearchStatus message)
        {
            loaderStoryboard.Begin(this, true);
        }

        /// I manually stop the animation before going to other screens
        private void stopAnimation_Click(object sender, System.Windows.RoutedEventArgs e)
        {
           loaderStoryboard.Stop(this);
        }

    }
}

视图模型

namespace TwoViews.ViewModels
{
    public class SecondViewModel : ViewModelBase
    {
        private string _inputFileName;

        public string InputFileNameChanged
        {
            get { return _inputFileName; }
            set {
                // send message to start animation everytime the textBox changes
                Messenger.Default.Send<MessageSearchStatus>(new MessageSearchStatus { isSearchingFile = true });
            }
        }
    }
}

请注意,在我的代码中,我没有显示停止动画的代码。

同样,动画工作正常,直到用户离开动画所在的页面并返回。

谢谢!

c# wpf mvvm mvvm-light
1个回答
0
投票

仅供参考 - 问题似乎是我在动画中使用的Storyboard。删除了Storyboard,一切正常。

SecondView.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Shapes;
using TwoViews.Models;
using System.Windows.Media;

namespace TwoViews.Views
{
    public partial class SecondView : UserControl
    {
        private DoubleAnimation myDoubleAnimation;

        public SecondView()
        {
            InitializeComponent();

            Messenger.Default.Register<MessageSearchStatus>(this, messageSearchStatus => ReceivedSearchStatus(messageSearchStatus));

            /// Animation for loader
            myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = 100;
            myDoubleAnimation.To = 0;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(.1));
            myDoubleAnimation.AutoReverse = true;
            myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
        }

        private void ReceivedSearchStatus(MessageSearchStatus message)
        {
            rectangleLoader.BeginAnimation(Rectangle.WidthProperty, myDoubleAnimation);
        }

        private void stopAnimation_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            rectangleLoader.BeginAnimation(Rectangle.WidthProperty, null);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.