c# - 在 visual studio community 2017 中找不到类型 ObservableDictionary

标签 c# visual-studio xaml

在包含以下代码的 Visual Studio Community MainPage.xaml.cs 文件中:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Animation;
using System.Collections.ObjectModel;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App2
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        Random random = new Random();
        ///private NavigationHelper navigationHelper; ///not needed in Visual Studio 2017
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            AddEnemy();
        }

        private void AddEnemy()
        {
            ContentControl enemy = new ContentControl();
            enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
            AnimateEnemy(enemy, 0, playArea.ActualWidth - 100, "(Canvas.Left)");
            AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100), random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
            playArea.Children.Add(enemy);
        }

        private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
        {
            Storyboard storyboard = new Storyboard() { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
            DoubleAnimation animation = new DoubleAnimation()
            {
                From = from,
                To = to,
                Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
            };

            Storyboard.SetTarget(animation, enemy);
            Storyboard.SetTargetProperty(animation, propertyToAnimate);
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }
    }
}

...行

private ObservableDictionary defaultViewModel = new ObservableDictionary();

产生以下错误信息:

Error   CS0246  The type or namespace name 'ObservableDictionary' could not 
be found (are you missing a using directive or an assembly reference?)

我应该包含什么库才能消除此错误消息?

最佳答案

我知道这个问题太老了,但是 Head First C# 使用了一个 Common 文件夹,里面有这个自述文件:

The Common directory contains classes and XAML styles that simplify application development.

These are not merely convenient, but are required by most Visual Studio project and item templates. If you need a variation on one of the styles in StandardStyles it is recommended that you make a copy in your own resource dictionary. When right-clicking on a styled control in the design surface the context menu includes an option to Edit a Copy to simplify this process.

Classes in the Common directory form part of your project and may be further enhanced to meet your needs. Care should be taken when altering existing methods and properties as incompatible changes will require corresponding changes to code included in a variety of Visual Studio templates. For example, additional pages added to your project are written assuming that the original methods and properties in Common classes are still present and that the names of the types have not changed.

在 VS2013 解决方案公共(public)文件夹中(也有一个 VS2012,具有完全不同的类)这些是 cs 文件: enter image description here

要使用这些类,您必须添加:

using Save_the_Humans.Common;

HFC# 从未在书中引用或提及这些类别;我假设他们当时是样板代码;他们包装了一些接口(interface)和字典:

public class ObservableDictionary : IObservableMap<string, object>
{
    private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs<string>
    {
        public ObservableDictionaryChangedEventArgs(CollectionChange change, string key)
        {
            this.CollectionChange = change;
            this.Key = key;
        }

        public CollectionChange CollectionChange { get; private set; }
        public string Key { get; private set; }

...

您可以从 them 获取 C# 代码 - 不仅是公共(public)文件夹 -也使用 VS2012 版本或来 self 的 repository只有 VS2013;从他们的存储库中,您无法打开或导入到 VS2017 - 至少我不能;我的已经是用 VS2017 制作的了。

希望对大家有帮助。

关于c# - 在 visual studio community 2017 中找不到类型 ObservableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43970271/

相关文章:

c# - 如何从 C++/C# 与 silverlight 网站交互

c# - 防止 XAML/C# 中 ListView 的 Return(Enter)、向上和向下箭头键的默认行为 (Windows 10)

c# - Navigation Back & MVVM - 如何刷新 WP8 页面数据绑定(bind)

c# - 将动态创建的位图对象绑定(bind)到 WPF 中的图像

c# - 如何在 Razor MVC asp.net 中组合两个 View 模型

c# Web 服务引用 c# 项目引用 DLL

c++ - Cuda NVCC 编译器 - 如何/showincludes?

c# - 如何在 C# 中制作一个不可见但可点击的按钮

C#正则表达式过滤字符

c# - Visual Studio 2012 代码分析错误 CA0058