我有一个Windows应用程序,它将在Windows XP和更新版本(即Vista/7)中运行.根据Vista UI准则,标准尺寸为16x16,32x32,48x48,256x256(XP标准尺寸不包括256x256图标).除了那些尺寸,我还有96x96和128x128(可以创造更多).
我应该包含以下哪些图标尺寸?shell实际上会使用"非标准"大小,还是只是膨胀我的应用程序?
在更新我的UI代码(.NET 4.0应用程序中的C#)时,由于在错误的线程中执行了对UI的调用,我遇到了一个奇怪的崩溃.但是,我已经在主线程上调用了这个调用,所以崩溃没有任何意义:MainThreadDispatcher.Invoke(new Action(View.Method))
崩溃了"调用线程无法访问此对象,因为另一个线程拥有它." 在View属性上.
经过进一步调查,我找到了原因:我是通过方法组调用的.我原以为使用方法组或委托/ lambda基本上是一回事(另见这个问题和这个问题).相反,将方法组转换为委托会导致代码执行,检查值View
.这是立即完成的,即在原始(非UI)线程上,这导致崩溃.如果我使用lambda,则稍后检查属性,从而在正确的线程中完成.
至少可以说,这似乎很有趣.C#标准中有没有提到这个的地方?或者是因为需要找到正确的转换而隐含的?
这是一个测试程序.首先,直接的方式.其次,分两步,更好地说明会发生什么.为了获得额外的乐趣,我Item
在创建委托后进行修改.
namespace ConsoleApplication1 // Add a reference to WindowsBase to a standard ConsoleApplication
{
using System.Threading;
using System.Windows.Threading;
using System;
static class Program
{
static Dispatcher mainDispatcher;
static void Main()
{
mainDispatcher = Dispatcher.CurrentDispatcher;
mainDispatcher.Thread.Name = "Main thread";
var childThread = new Thread(() =>
{
Console.WriteLine("--- Method group ---");
mainDispatcher.Invoke(new Action(Item.DoSomething));
Console.WriteLine("\n--- Lambda ---");
mainDispatcher.Invoke(new Action(() => Item.DoSomething()));
Console.WriteLine("\n--- Method …
Run Code Online (Sandbox Code Playgroud) Moq允许模拟受保护的虚拟成员(参见此处).是否有可能在FakeItEasy中做同样的事情?
首先介绍一下:我有一个WPF应用程序,它是传统Win32应用程序的GUI前端.遗留应用程序在单独的线程中作为DLL运行.用户在UI中选择的命令在该"遗留线程"上调用.
如果"遗留线程"结束,GUI前端不能再做任何有用的事情了,所以我需要关闭WPF应用程序.因此,在线程的方法结束时,我打电话Application.Current.Shutdown()
.
由于我不在主线程上,我需要调用此命令.但是,我注意到Dispatcher也必须BeginInvokeShutdown()
关闭调度程序.所以我的问题是:调用之间有什么区别
Application.Current.Shutdown();
Run Code Online (Sandbox Code Playgroud)
并打电话
Application.Current.Dispatcher.BeginInvokeShutdown();
Run Code Online (Sandbox Code Playgroud) 我在我的.NET 4 WPF应用程序中使用WPF OpenFileDialog和SaveFileDialog.我使用Filter属性允许用户设置不同的文件过滤器.在.NET 4中,它使用Windows Vista引入的本机文件对话框(如果可能).
但是,这些对话框显示组成文件筛选器的扩展.由于其中一些使用了相当多的扩展,这非常难看.
例如,我有一个过滤器Image files|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.tif;*.tiff;*.png;*.ico|All files|*.*
,它Image files (*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.tif;*.tiff;*.png;*.ico)
在对话框中显示.括号中的所有内容都会自动添加,即根据Filter字符串显示Image files
.但在某处,括号中的内容被添加.我试着用Reflector查看代码,看看它是否在某个地方完成,但很快就放弃了,因为它很复杂.
例如,启动Paint,我可以看到可以使用这些文件对话框而不用括号中的东西,即它显示Image files
.
有没有人知道这个"功能"的解决方法?
这是我第一次使用它SafeHandle
.
我需要调用这个需要UIntPtr的P/Invoke方法.
Run Code Online (Sandbox Code Playgroud)[DllImport("advapi32.dll", CharSet = CharSet.Auto)] public static extern int RegOpenKeyEx( UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult);
这个UIntPtr将派生自.NET的RegistryKey类.我将使用上面的方法将RegistryKey类转换为IntPtr,以便我可以使用上面的P/Invoke:
private static IntPtr GetRegistryKeyHandle(RegistryKey rKey)
{
//Get the type of the RegistryKey
Type registryKeyType = typeof(RegistryKey);
//Get the FieldInfo of the 'hkey' member of RegistryKey
System.Reflection.FieldInfo fieldInfo =
registryKeyType.GetField("hkey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
//Get the handle held by hkey
if (fieldInfo != null)
{
SafeHandle handle = (SafeHandle)fieldInfo.GetValue(rKey);
//Get the unsafe handle
IntPtr dangerousHandle = handle.DangerousGetHandle(); …
Run Code Online (Sandbox Code Playgroud) 我有一个组合框,显示我们国家的不同市镇(这些市政当局属于特定省份).由于市政府名称相同,我将"MunicipalityName"(我的数据库中"MUNICIPALITY"表中的表格列)绑定到comboBox的DisplayMember属性,将"Municipality_ID"绑定到comboBox的ValueMember属性.
当用户保存他的详细信息时,我从MUNICIPALITY的ValueMember提供SelectedValue并将其插入Employee表.
cmd.Parameters.Add(new SqlParameter("@Municipality_ID", (object)comboBoxMunicipality.SelectedValue.ToString()));
Run Code Online (Sandbox Code Playgroud)
当员工需要更新他的信息时,我发现很难找到数据.我必须手动检查该员工的Municipality_ID并将其与comboBox中的绑定数据进行比较,然后遍历它,确定Municipality_ID所在的索引,并设置comboBox的SelectedIndex属性.(与下面的代码片段相比,安静冗长)
我有这个代码,但我发现冲突,因为Municipality_Name不是唯一的.
//set SelectedIndex based from DisplayMember of the comboBox
comboBoxMunicipality.SelectedIndex = comboBoxMunicipality.FindStringExact(dataTable.Rows[0]["MunicipalityName"].ToString());
Run Code Online (Sandbox Code Playgroud)
有没有办法像上面的代码一样设置comboBox的SelectedIndex,但这一次,将它与ValueMember相比较?
有捷径吗?
//something like this?
comboBoxMunicipality.SelectedIndex =
comboBoxMunicipality.FindByValue(dataTable.Rows[0]["Municipality_ID"].ToString());
Run Code Online (Sandbox Code Playgroud)
我希望你能得到我的观点...请帮助.谢谢.
在我的WPF应用程序中,我使用HwndHost托管Win32内容.但是,创建HwndHost不会创建本机窗口.相反,这是在重写的BuildWindowCore()
方法中完成的,该方法稍后由WPF调用.
我的托管内容需要本机窗口的窗口句柄以进行自己的初始化.不幸的是,我无法强制创建窗口(即让WPF调用BuildWindowCore),所以我有第二个线程轮询HwndHost,直到它被初始化.
在.NET 4.0/WPF 4.0中,WindowInteropHelper.EnsureHandle()
添加了一种新方法.我希望这可以解决这种情况,但它只适用于Window,而不适用于HwndHost(它不是从Window派生的).你有建议我可以做什么吗?
编辑:
我忘了为可能的解决方案添加更多约束:
在我的WPF应用程序中,我有一个TextBox,用户可以在其中输入百分比(int,介于1和100之间).Text属性数据绑定到ViewModel中的属性,在此处我将值强制置于setter中的给定范围内.
但是,在.NET 3.5中,强制后,UI中的数据无法正确显示.在MSDN上的这篇文章中,WPF博士表示您必须手动更新绑定,以便显示正确的内容.因此,我有一个TextChanged
调用处理程序(在View中)UpdateTarget()
.在代码中:
查看XAML:
<TextBox Text="{Binding Percentage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue={x:Static sys:String.Empty}}"
TextChanged="TextBox_TextChanged"/>
Run Code Online (Sandbox Code Playgroud)
查看代码隐藏:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
// Removed safe casts and null checks
((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}
Run Code Online (Sandbox Code Playgroud)
视图模型:
private int? percentage;
public int? Percentage
{
get
{
return this.percentage;
}
set
{
if (this.Percentage == value)
{
return;
}
// Unset = 1
this.percentage = value ?? 1;
// Coerce to be between 1 and 100.
// Using the TextBox, a user …
Run Code Online (Sandbox Code Playgroud) 我有这个:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
<GridSplitter Background="{x:Static SystemColors.ControlBrush}"
Grid.Column="1"
Margin="0,0,0,0"
Name="splitter"
HorizontalAlignment="Stretch" />
Run Code Online (Sandbox Code Playgroud)
我正在尝试保存并恢复分离器位置.我正在使用grid.ColumnDefinitions[0].Width
,它以像素为单位返回列的宽度.
当我恢复位置时,如何恢复并保持50*设置,以便在调整窗口大小时,列可以正确调整大小?
c# ×5
wpf ×5
.net ×2
.net-4.0 ×2
wpf-4.0 ×2
.net-3.5 ×1
data-binding ×1
desktop ×1
dispatcher ×1
fakeiteasy ×1
handles ×1
hwndhost ×1
icons ×1
method-group ×1
pinvoke ×1
shutdown ×1
unit-testing ×1
windows ×1
windows-7 ×1
winforms ×1