我收到错误消息:在MVVMLight应用程序中在页面之间移动时,在以下名称范围内找不到名称

时间:2018-06-23 15:12:02

标签: c# wpf mvvm mvvm-light

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

在第二页中,我有一个加载器动画,每次在textBox字段中键入内容时都会触发该动画。一切正常。从第一页开始,我可以转到第二页,然后在textBox中键入一些内容,然后动画开始。问题是,如果我转到第二页,则返回第一页,然后再次转到第二页,并在textBox中键入内容,我得到一个错误,指出加载程序不存在,有趣的是,直到离开页面并返回时我才收到此错误。

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

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

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

  1. 下载并打开应用程序。

  2. 转到第2页。

  3. 在文本框中输入内容(动画应开始)。

  4. 返回第1页,什么也不做。

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

错误:

  
    

其他信息:在“ 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);
        }

    }
}

ViewModel

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 });
            }
        }
    }
}

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

同样,动画效果良好,直到用户离开动画所在的页面并返回。

谢谢!

1 个答案:

答案 0 :(得分: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);
        }
    }
}