积极答复者
关于StaticResource的问题?????

问题
-
在msdn下载的教程里,我看到一个关于AutoCompleteBox 例子 <toolkit:AutoCompleteBox HorizontalAlignment="Left" Margin="32,194,0,0" Name="autoCompleteBox1" VerticalAlignment="Top" Width="382" Height="161" ItemsSource="{StaticResource words}" MaxDropDownHeight="50" IsHitTestVisible="False" />
里面StaticResource words是怎么生成的,我看到是由两个文件MyTexts.txt,MyTexts.cs文件形成,但是我不知道这两个文件是如何形成StaticResource ,以让系统可以使用,什么时候编译
和xmlns:data="clr-namespace:Demo5.Data"这个有关么?
我如何形成类似的StaticResource 以便维护,有什么特别的要求和步骤没,其他的StaticResource 可以这样写么????
2012年2月3日 7:38
答案
-
你好 ItemsSource="{StaticResource words} 这个是XAML数据绑定的语言
words应该是一个集合对象,它可能是在当前空间/当前Page/程序资源中定义的一个读取你的文本文件字符串的继承于集合类的对象的实例,比如
<local:MyWords x:name="word"/>
local是包含MyWords类的命名空间。
参考 http://msdn.microsoft.com/zh-cn/magazine/cc700358.aspx
http://developer.51cto.com/art/200808/87104.htm
Raymond Tang (Microsoft C# MVP)
Denn Ich Gehoer nur mir
微软中文论坛同城社区成都QQ群:74268428
My Blog http://kosmisch.net
Chengdu,China- 已标记为答案 第几个程序员 2012年2月8日 1:59
2012年2月3日 13:31
全部回复
-
这是文件的代码:
.txt:
China Chinese Chinaman chinaberry
baa baal
aa aaa aaal
.cs:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Resources;
using System.IO;
namespace Demo5.Data
{
public static class Extensions
{
/// <summary>
/// Return a random item from a list.
/// </summary>
/// <typeparam name="T">The item type.</typeparam>
/// <param name="rnd">The Random instance.</param>
/// <param name="list">The list to choose from.</param>
/// <returns>A randomly selected item from the list.</returns>
public static T Next<T>(this Random rnd, IList<T> list)
{
return list[rnd.Next(list.Count)];
}
}
public class MyText : IEnumerable<string>
{
public enum Capitalization
{
None,
FirstWord,
AllWords
}
private static Random _rnd = new Random(42);
private static StringBuilder _builder = new StringBuilder();
private static List<string> _sentences;
private static List<string> _words;
/// <summary>
/// Returns random lorem ipsum sentences combined into a single string.
/// </summary>
/// <param name="sentenceCount">The nunmber of sentences.</param>
/// <returns>The paragraph, composed of random sentences.</returns>
public static string GetParagraph(int sentenceCount)
{
EnsureSentences();
_builder.Length = 0;
while (sentenceCount-- > 0)
{
_builder.Append(_rnd.Next(_sentences));
if (sentenceCount > 0)
{
_builder.Append(' ');
}
}
return _builder.ToString();
}
/// <summary>
/// Return an alphabetized, lower-case list of lorem ipsum words.
/// </summary>
public static ICollection<string> Words
{
get
{
EnsureWords();
return (ICollection<string>)_words;
}
}
/// <summary>
/// Get a string composed of random lorem ipsum words. Will not end with punctuation.
/// </summary>
/// <param name="wordCount">Number of words.</param>
/// <param name="capitalize">How to capitalize the words.</param>
/// <returns></returns>
public static string GetWords(int wordCount, Capitalization capitalization)
{
EnsureWords();
_builder.Length = 0;
while (wordCount-- > 0)
{
int position = _builder.Length;
_builder.Append(_rnd.Next(_words));
if (capitalization == Capitalization.AllWords || (position == 0 && capitalization == Capitalization.FirstWord))
{
_builder[position] = char.ToUpper(_builder[position]);
}
if (wordCount > 0)
{
_builder.Append(' ');
}
}
return _builder.ToString();
}
/// <summary>
/// Enumerates the Words property.
/// </summary>
/// <returns>The enumerator.</returns>
public IEnumerator<string> GetEnumerator()
{
return MyText.Words.GetEnumerator();
}
/// <summary>
/// Enumerates the Words property.
/// </summary>
/// <returns>The enumerator.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return MyText.Words.GetEnumerator();
}
/// <summary>
/// Reads the lorem ipsum sentences. Supplies some data in case reading fails, which
/// it will do at design time.
/// </summary>
private static void EnsureSentences()
{
if (_sentences == null)
{
_sentences = new List<string>();
StreamResourceInfo info = Application.GetResourceStream(new Uri("MyTexts.txt", UriKind.Relative));
if (info != null)
{
Stream stream = info.Stream;
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
_sentences.Add(reader.ReadLine());
}
};
}
}
if (_sentences.Count == 0)
{
_sentences.Add("no words");
}
}
}
/// <summary>
/// Creates an alphabetized list of the words from the lorem ipsum text.
/// </summary>
private static void EnsureWords()
{
char[] separators = { ' ', ',', '.' };
EnsureSentences();
if (_words == null)
{
Dictionary<string, object> temp = new Dictionary<string, object>();
foreach (string sentence in _sentences)
{
string[] words = sentence.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words)
{
temp[word.ToLower()] = null;
}
}
_words = new List<string>(temp.Keys);
_words.Sort();
}
}
}
}
2012年2月3日 7:46 -
你好 ItemsSource="{StaticResource words} 这个是XAML数据绑定的语言
words应该是一个集合对象,它可能是在当前空间/当前Page/程序资源中定义的一个读取你的文本文件字符串的继承于集合类的对象的实例,比如
<local:MyWords x:name="word"/>
local是包含MyWords类的命名空间。
参考 http://msdn.microsoft.com/zh-cn/magazine/cc700358.aspx
http://developer.51cto.com/art/200808/87104.htm
Raymond Tang (Microsoft C# MVP)
Denn Ich Gehoer nur mir
微软中文论坛同城社区成都QQ群:74268428
My Blog http://kosmisch.net
Chengdu,China- 已标记为答案 第几个程序员 2012年2月8日 1:59
2012年2月3日 13:31