C# I get error:在MVVMLight应用程序中从一页移动到另一页时,在的名称范围中找不到名称

C# I get error:在MVVMLight应用程序中从一页移动到另一页时,在的名称范围中找不到名称,c#,wpf,mvvm,mvvm-light,C#,Wpf,Mvvm,Mvvm Light,我有一个MVVMLight多页应用程序,它有两个页面,我可以通过单击相应的按钮从一个页面导航到另一个页面 在第二页中,我有一个加载程序动画,每次在文本框字段中键入内容时都会触发该动画。一切正常;我可以从第一页转到第二页,然后在文本框中键入一些内容,动画开始。问题是,如果我转到第二页,然后我回到第一页,然后我再转到第二页,在文本框中键入一些内容,我会得到一个错误,说加载程序的名称不存在,有趣的是,直到我离开该页并回来,我才得到这个错误 你知道为什么动画在离开页面回来后会停止工作吗 编辑:这里是指向

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

在第二页中,我有一个加载程序动画,每次在
文本框
字段中键入内容时都会触发该动画。一切正常;我可以从第一页转到第二页,然后在
文本框中键入一些内容,动画开始。问题是,如果我转到第二页,然后我回到第一页,然后我再转到第二页,在
文本框中键入一些内容,我会得到一个错误,说加载程序的名称不存在,有趣的是,直到我离开该页并回来,我才得到这个错误

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

编辑:这里是指向完整项目的链接。

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

  • 下载并打开应用程序

  • 转到第2页

  • 在文本框中键入内容(动画应开始)

  • 回到第一页,什么也不做

  • 再次转到第2页,尝试在文本框中键入内容(您应该在此处看到错误)

  • 错误: 其他信息:“TwoView.Views.SecondView”的名称范围中找不到“rectangleLoader”名称

    XAML
    
    
    SecondView.xaml.cs
    namespace twowiews.Views
    {
    公共部分类SecondView:UserControl
    {
    私人故事板加载程序;
    公共第二视图()
    {
    初始化组件();
    Register(this,messageSearchStatus=>receivedsearchtstatus(messageSearchStatus));
    ///装载机动画
    DoubleAnimation myDoubleAnimation=新的DoubleAnimation();
    myDoubleAnimation.From=100;
    myDoubleAnimation.To=0;
    myDoubleAnimation.Duration=新的持续时间(TimeSpan.FromSeconds(.1));
    myDoubleAnimation.AutoReverse=true;
    myDoubleAnimation.RepeatBehavior=RepeatBehavior.Forever;
    loaderStoryboard=新故事板();
    loaderStoryboard.Children.Add(myDoubleAnimation);
    Storyboard.SetTargetName(myDoubleAnimation,rectangleLoader.Name);
    Storyboard.SetTargetProperty(myDoubleAnimation,新属性路径(Rectangle.WidthProperty));
    }
    private void ReceivedSearchStatus(消息搜索状态消息)
    {
    loaderStoryboard.Begin(这是真的);
    }
    ///在转到其他屏幕之前,我手动停止动画
    私有void stop动画\u单击(对象发送器,System.Windows.RoutedEventArgs e)
    {
    装载机板。停止(此);
    }
    }
    }
    
    视图模型
    namespace twowiews.ViewModels
    {
    公共类SecondViewModel:ViewModelBase
    {
    私有字符串_inputFileName;
    公共字符串InputFileName已更改
    {
    获取{return\u inputFileName;}
    设置{
    //每次文本框更改时发送消息以启动动画
    Send(新的MessageSearchStatus{isSearchingFile=true});
    }
    }
    }
    }
    

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

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


    谢谢

    FYI-问题似乎是我在动画中使用的
    情节提要。删除了
    情节提要
    ,一切正常

    SecondView.xaml.cs
    使用System.Windows;
    使用System.Windows.Controls;
    使用System.Windows.Media.Animation;
    使用制度;
    使用System.Windows.Input;
    使用GalaSoft.MvvmLight.Messaging;
    使用GalaSoft.MvvmLight.Threading;
    使用System.Collections.Generic;
    使用System.Collections.ObjectModel;
    使用System.IO;
    使用System.Collections.Specialized;
    使用System.Linq;
    使用System.Windows.Shapes;
    使用TwoView.Models;
    使用System.Windows.Media;
    命名空间视图。视图
    {
    公共部分类SecondView:UserControl
    {
    私人双动画myDoubleAnimation;
    公共第二视图()
    {
    初始化组件();
    Register(this,messageSearchStatus=>receivedsearchtstatus(messageSearchStatus));
    ///装载机动画
    myDoubleAnimation=新的DoubleAnimation();
    myDoubleAnimation.From=100;
    myDoubleAnimation.To=0;
    myDoubleAnimation.Duration=新的持续时间(TimeSpan.FromSeconds(.1));
    myDoubleAnimation.AutoReverse=true;
    myDoubleAnimation.RepeatBehavior=RepeatBehavior.Forever;
    }
    private void ReceivedSearchStatus(消息搜索状态消息)
    {
    rectangleLoader.BeginAnimation(Rectangle.WidthProperty,myDoubleAnimation);
    }
    私有void stop动画\u单击(对象发送器,System.Windows.RoutedEventArgs e)
    {
    rectangleLoader.BeginAnimation(Rectangle.WidthProperty,null);
    }
    }
    }
    
    “请注意,在我的代码中,我没有显示停止动画的代码。”为什么不显示?如果您希望任何人能够指出您的问题所在,您应该提供您的问题的MCVE:我没有发布它,因为错误将我指向用于表示加载器的矩形,所以我认为它与此无关,但现在我确实相信导致错误的原因是
    动画
    ,因为我尝试了在方法中访问“rectangleLoader”的其他属性,在这里我得到了错误,但没有得到任何错误。顺便说一句,在转到其他v之前,我将手动停止动画
    <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>
    
    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 });
                }
            }
        }
    }
    
    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);
            }
        }
    }