gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:09 24 4
gpt4 key购买 nike

在包含以下代码的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com