本文主要翻译:http://visuallylocated.com/post/2015/02/20/Creating-a-WrapPanel-for-your-Windows-Runtime-apps.aspx http://depblog.weblogs.us/2015/02/18/how-to-add-a-tag-list-into-winrt-universal-apps/
我们需要给用户很多标签,我们需要使用一个控件,他的长度是变化,可以快速放,这样好像wrapPancel就是我们需要,因为这个我直接写如果看起来不懂,可以看
我们点添加就会添加标签,我们删除标签就很快排版。
我们使用RichBox,这个可以做我们标签
源代码因为作者写的和UWP不一样,我改UWP,放在https://github.com/lindexi/TagList
运行效果
点击按钮
删除
软件使用,先add
跳到让用户选择,这里如果让用户输入,使用有点难,可以使用用户在跳转输入,输入自动变为预设一样
源.Add(new Tag() {Id = "id",Label = "用户输入"});
选择标签,选择完成保存
可以看到首页
标签使用在跳转MainPage
if (e.NavigationMode == NavigationMode.Back)
我们把选择保存
General.GetInstance().TagSelection
在SetTags
是本算法的主要
我们搜索全部新加和被删除
var tagParagraph = (Paragraph) (from paragraph in TagRichTextBlock.Blocks where paragraph.Name.StartsWith("Tags") select paragraph).FirstOrDefault();
var tagIds = from tag in General.GetInstance().TagSelection.Tags select tag.Id;
var buttonsToRemove = from item in tagParagraph.Inlines.Cast<InlineUIContainer>() where !tagIds.Contains(((Button) item.Child).Name) select item;
foreach (InlineUIContainer container in buttonsToRemove) tagParagraph.Inlines.Remove(container);
IEnumerable<string> buttonIds = from item in tagParagraph.Inlines.Cast<InlineUIContainer>() select ((Button) item.Child).Name;
IEnumerable<Tag> tagsToAdd = from item in General.GetInstance().TagSelection.Tags where !buttonIds.Contains(item.Id) select item;
foreach (Tag tag in tagsToAdd) { InlineUIContainer container = new InlineUIContainer(); RichTextBlock inlineRichTextBlock = new RichTextBlock() { IsTextSelectionEnabled = false }; Paragraph inlineParagraph = new Paragraph(); inlineParagraph.Inlines.Add(new Run() { Text = string.Format("{0} ", tag.Label), FontSize = 14 }); inlineParagraph.Inlines.Add(new Run() { Text = "\uE106", FontFamily = new FontFamily("Segoe UI Symbol"), FontSize = 10 }); inlineRichTextBlock.Blocks.Add(inlineParagraph);
Button tagButton = new Button() { Content = inlineRichTextBlock, Style = (Style) Application.Current.Resources["TagButtonStyle"], Name = tag.Id }; tagButton.Click += OnTagButtonClicked; container.Child = tagButton;
tagParagraph.Inlines.Add(container); }
点击删除按钮,删除id
string tagId = ((Button) sender).Name; General.GetInstance() .TagSelection.Tags.Remove(General.GetInstance().TagSelection.Tags.Single(item => item.Id.Equals(tagId))); SetTags();
源码:https://github.com/Depechie/TagList

原文链接: http://blog.lindexi.com/post/win-10-UWP-%E6%A0%87%E7%AD%BE
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。 欢迎转载、使用、重新发布,但务必保留文章署名 林德熙 (包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我 联系。