- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 MVVM light 创建我的第一个 C# UWP 桌面应用程序,我在内存使用和 DataTemplates 方面遇到了一些小问题。在 Button.cs 中有一个 ObservableCollection,它可能有不同的对象(IExample),例如:圆形、正方形和其他...根据该集合中的内容,应显示适当的 UI。
这是我的代码:
按钮.cs:
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
namespace MvvmLight1.Model
{
public class Button : ObservableObject
{
public string Name { get; set; }
public const string InternalObjectsPropertyName = "InternalObjects";
private ObservableCollection<IExample> _internalObjects = null;
public ObservableCollection<IExample> InternalObjects
{
get
{
return _internalObjects;
}
set
{
if (_internalObjects == value)
{
return;
}
_internalObjects = value;
RaisePropertyChanged(InternalObjectsPropertyName);
}
}
public Button()
{
InternalObjects = new ObservableCollection<IExample>();
}
}
}
主页.xaml:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MvvmLight1"
xmlns:ignore="http://www.galasoft.ch/ignore"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:local1="using:MvvmLight1.Model"
x:Class="MvvmLight1.MainPage"
mc:Ignorable="d ignore"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Page.Resources>
<!-- template for circle -->
<DataTemplate x:Key="CircleTemplate">
<StackPanel>
<TextBlock Text="This is the template for circle..." Foreground="Crimson" HorizontalAlignment="Center"></TextBlock>
<ColorPicker x:Name="myColorPicker" ColorSpectrumShape="Ring"/>
</StackPanel>
</DataTemplate>
<!-- template for square -->
<DataTemplate x:Key="SquareTemplate">
<StackPanel>
<TextBlock Text="This is the template for square..." Foreground="Green" HorizontalAlignment="Center"></TextBlock>
<ColorPicker x:Name="myColorPicker" ColorSpectrumShape="Ring"/>
</StackPanel>
</DataTemplate>
<local1:TemplateSelector x:Key="MySelector"
CircleTemplate="{StaticResource CircleTemplate}"
SquareTemplate="{StaticResource SquareTemplate}"
/>
</Page.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- buttons -->
<GridView ItemsSource="{Binding ButtonsList}" Grid.Row="0" SelectedItem="{Binding SelectedButton, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Foreground="Red"
FontSize="15" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<!-- properties -->
<ItemsControl ItemsSource="{Binding InternalObjects, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplateSelector="{StaticResource MySelector}" DataContext="{Binding SelectedButton}" Grid.Column="1"></ItemsControl>
</Grid>
public class MainViewModel : ViewModelBase
{
public const string ButtonsListPropertyName = "ButtonsList";
private ObservableCollection<Button> _buttonsList = null;
public ObservableCollection<Button> ButtonsList
{
get
{
return _buttonsList;
}
set
{
if (_buttonsList == value)
{
return;
}
_buttonsList = value;
RaisePropertyChanged(ButtonsListPropertyName);
}
}
public const string SelectedButtonPropertyName = "SelectedButton";
private Button _selectedButton = null;
public Button SelectedButton
{
get
{
return _selectedButton;
}
set
{
if (_selectedButton == value)
{
return;
}
_selectedButton = value;
RaisePropertyChanged(SelectedButtonPropertyName);
}
}
public MainViewModel()
{
ButtonsList = new ObservableCollection<Button>();
// create new buttons
for (var i = 0; i < 12; i++)
{
ButtonsList.Add(new Button());
}
// for few of them add circles...
for (var i = 0; i < 3; i++)
{
ButtonsList[i].Name = "Button with Circle";
ButtonsList[i].InternalObjects.Add(new Circles() { Name = "Circles" });
}
// for another add squares...
for (var i = 3; i < 6; i++)
{
ButtonsList[i].Name = "Button with Squres";
ButtonsList[i].InternalObjects.Add(new Squares() { Name = "Squares" });
}
// rest of them mixed...
for (var i = 6; i < 12; i++)
{
ButtonsList[i].Name = "Button with Circles and Squres";
ButtonsList[i].InternalObjects.Add(new Circles() { Name = "Circles" });
ButtonsList[i].InternalObjects.Add(new Squares() { Name = "Squares" });
}
}
}
Circles.cs:
public class Circles : IExample
{
public string Name { get; set; }
}
正方形.cs:
public class Squares : IExample
{
public string Name { get; set; }
}
模板选择器.cs:
public class TemplateSelector : DataTemplateSelector
{
public DataTemplate CircleTemplate { get; set; }
public DataTemplate SquareTemplate { get; set; }
protected override DataTemplate SelectTemplateCore(object item)
{
if (item.GetType() == typeof(Circles))
{
return CircleTemplate;
}
else if (item.GetType() == typeof(Squares))
{
return SquareTemplate;
}
else
{
return null;
}
}
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
return SelectTemplateCore(item);
}
}
我做错了什么?为什么内存从 19mb 增长到 91mb?
[编辑]:完整的解决方案在这里:download
最佳答案
每次您选择一个按钮时,DataTemplateSelector 都会从您的圆形和方形模板创建新的控件。我能够通过将模板添加到 ItemsControl 的资源并在每次更新 InternalObjects-Collection 时创建/处置控件来避免这种情况:
<ItemsControl ItemsSource="{Binding InternalObjects, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding SelectedButton}" Grid.Column="1">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Circles}">
<TextBlock Text="This is the template for a circle too..." Foreground="Crimson" HorizontalAlignment="Center"></TextBlock>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Squares}">
<TextBlock Text="This is the template for a square too..." Foreground="Crimson" HorizontalAlignment="Center"></TextBlock>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
使用 ComboBox 对其进行测试:
<ComboBox ItemsSource="{Binding ButtonsList}" Grid.Row="0" SelectedItem="{Binding SelectedButton, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="x:String">
<TextBlock Text="{Binding Name}" Foreground="Red" FontSize="15" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Center"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
根据模板的大小,它可能仍会消耗大量内存。
要考虑的另一个选择是从模板中提取控件并创建它的单个实例(如果可能,例如 ColorPicker)。
否则您需要依赖 C#/UWP 来为您管理创建的控件。
关于C# UWP DataTemplate 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51876432/
uwp 和非 uwp 应用程序之间是否可以进行通信。我的非uwp是一个提供服务的后台任务。我想在 uwp 应用程序中调用该非 uwp 服务。 如何调用电话? AppServiceConnection
我正在开发一个应用程序,该应用程序旨在在具有多个显示器(从 1 到 3必要)在每个监视器上,每个监视器都显示不同的 View 。 就我所见,UWP 并不自然适用于这种情况。我设法使用 CoreAppl
我正在尝试对UWP应用使用broadFileSystemAccess功能,但是package.appxmanifest的功能列表中未列出broadFileSystemAccess功能。 我的最低和最高
有时我有一个打开的流或一个事件的队列,必须在应用程序关闭时正确处理。在 WPF 应用程序中,我可以在应用程序上使用 Unloading 事件,但在 UWP 中我注意到这样的事件不存在。 我如何可靠地做
http://wikisend.com/download/880354/UWP_Server.zip 我已将代码上传至上述网址。 它是 UWP 中的客户端和服务器应用程序。这里客户端和服务器都在同一个
大家好 我知道这个问题(Chromium working on UWP)之前(2015/2016)有人问过,想看看有没有更新 我正在尝试在 UWP 应用程序中使用 CEF3 构建,但在运行该应用程序时
我目前正在构建一个应用程序,它可以使用 Windows 游戏 DVR 在某个时刻开始录制屏幕。该录音机在完成录音时将应用程序名称作为文件名。 我发现了如何使用 Applicationview.GetF
我已使用 Desktop App Converter 将我的 WPF 应用程序转换为 appx 包。我需要在资源管理器上下文菜单中有一个项目。 IE。用户右键单击文件并在主菜单中看到我的项目“对应用程
我想稍微修改一个 Button 控件(添加描述)。在哪里可以找到 UWP 控件的默认控件模板? 最佳答案 似乎可以在以下位置找到它们: C:\Program Files (x86)\Windows K
我想通过 UWP 应用访问 windows10 注册表项。 键为:\HKEY_LOCAL_MACHINE\SOFTWARE\MyCompanyName\MyName 我找不到任何函数调用来完成它。 请
我开发了一个 UWP appx,它可以安装在 cmd.exe 提示符中: C:\test>myapp.appx 但是在安装过程中会弹出一个 Windows GUI。 有没有什么方法 使用 Silent
在我的 UWP 应用程序中,如何通过 UpdateTask 中的代码进行调试? VS 2017 中的“生命周期事件”下拉菜单似乎没有提供触发此类后台任务的选项。有办法实现吗? 最佳答案 首先是关于 U
我尝试在 VS 2017 中创建一个 UWP 应用程序包。 创建时我收到一条神秘的错误消息:严重性代码描述项目文件行抑制状态错误 0xdef00532 - 资源“Files/Assets/Square
我有一个 TextBlock在我的应用程序中。我要办理pinch in & out在它上面调整字体的大小。当ManipulationDelta事件触发我检查Scale属性(property),但大多数
为什么默认选择的索引不起作用?它因平台异常而崩溃: RumTime 错误: Exception thrown at 0x00007FFDEF7F7788 (KernelBase.dll) in ab
有没有办法在同一个包中的 UWP 应用程序和桌面桥应用程序之间共享互斥锁?它们似乎有不同的命名空间;使用相同的名称不会在进程之间产生相同的对象。根据 WinObj,UWP 应用程序的对象是,存储在 A
有什么方法可以检测当前的 UWP 要退出 ? (由用户关闭或终止进程) 我想向服务器发送一些关于应用程序将断开连接的请求,还想在退出之前保存一些数据。 最佳答案 无法检测这种情况或阻止用户终止您的应用
我正在使用 XAML 和 C# 开发通用 Windows 平台应用程序。我想在 UWP 中更改焦点上 TextBox 的边框颜色。 在此先感谢您的帮助。 最佳答案 其实实现起来很简单,按照以下步骤即可
是否可以在 UWP 应用中更改甚至隐藏鼠标指针? 我唯一能找到的是: Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = null; 但
我是一名优秀的程序员,十分优秀!