我有一个带有窗口的 WPF 项目。如果我想在 C# 中设置窗口的图标属性,它允许我仅向其提供 ImageSource,并且它不接受图标文件。如果我在 XAML 中设置该属性,它会毫无问题地接受图标文件。如何通过C#代码将WPF窗口的图标设置为图标文件?
我在窗口资源中有一些动画作为故事板.
有没有办法将它们移动到一个单独的文件,仍然可以访问它们?如果是,请告诉我如何.
为了清楚起见,我想将以下生成的代码从我的MainWindow.xaml文件移动到一个单独的文件中,这样我就可以保持代码整洁有序:
<Window.Resources>
<Storyboard x:Key="sbShowWindow">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="layoutRoot">
<EasingDoubleKeyFrame KeyTime="0" Value="0.874">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="layoutRoot">
<EasingDoubleKeyFrame KeyTime="0" Value="0.874">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="layoutRoot">
<EasingDoubleKeyFrame KeyTime="0" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="layoutRoot">
<EasingDoubleKeyFrame KeyTime="0" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"> …
Run Code Online (Sandbox Code Playgroud) 有没有更简洁的方法来检查x是"a","b","c","d"还是"e"?
if (x == "a" | x == "b" | x == "c" | x == "d" | x == "e"){//do something}
Run Code Online (Sandbox Code Playgroud)
基本上我想知道我是否可以表达相同的if语句而不重复变量名x.
如何检查当前活动表单的任何控件(复选框/单选按钮)是否已被选中/切换?
我的目标是创建一个名为选项更改保存自动这将使节能的当前状态check boxes
和radio buttons
,所以我需要在选择/切换任何控制要知道(执行保存设置方法).我不想为每个控件创建一个单独的事件处理程序,如果可能的话,我正在寻找一个通用的解决方案.
我创建了以下类:
using System.Windows;
using System.Windows.Input;
using MyUtils.MyArgParser;
namespace MyUtils.MyImplement
{
public static class ImplementExitOnEscape
{
#region ImplementExitOnEscape
public static void Implement(Window window)
{
window.KeyDown += Window_KeyDown;
}
private static void Window_KeyDown(object sender, KeyEventArgs e)
{
var window = sender as Window;
// Close window when pressing the escape key.
if (e.Key == Key.Escape) if (window != null) window.Close();
var optionX = MyArgParser.MyArgParser.GetOptionValue("optionX");
}
#endregion //ImplementExitOnEscape
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我不得不使用的全名空间MyArgParser
类var optionX = MyArgParser.MyArgParser.GetOptionValue("optionX");
,而不是仅仅MyArgParser.GetOptionValue("optionX");
?
using MyUtils.MyArgParser;
被忽略了.在那里使用它不会有任何区别,编译器仍然强迫我使用完整的命名空间. …
在proxy.exe中,我通过以下方式创建安全字符串:
public SecureString GetSecureEncryptionKey()
{
string strPassword = "8charPwd";
SecureString secureStr = new SecureString();
if (strPassword.Length > 0)
{
foreach (var c in strPassword.ToCharArray()) secureStr.AppendChar(c);
}
return secureStr;
}
Run Code Online (Sandbox Code Playgroud)
然后在main.exe中我使用此函数解密它:
public string convertToUNSecureString(SecureString secstrPassword)
{
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secstrPassword);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是返回的字符串是空的,除非我加密main.exe中的初始字符串,然后返回的解密字符串确实是"8charPwd".为什么会这样?SecureString加密是否绑定到可执行文件?
使用 Discord.Net 1.0.1
我想使用 UserUpdated 事件来检测用户何时更改其状态,但似乎无论用户进行了哪些更改(例如更改状态、昵称、头像等),这甚至都不会触发
示例代码:
this._client = new DiscordSocketClient(config: config);
this._client.UserUpdated += this.Client_OnUserUpdated;
private Task Client_OnUserUpdated(SocketUser arg1, SocketUser arg2)
{
Console.WriteLine(value: "Client_OnUserUpdated");
return Task.CompletedTask;
}
Run Code Online (Sandbox Code Playgroud)
当用户更改其属性时,如何触发事件?
当主窗体加载时,我试图设置一个键事件处理程序,如下所示:
private void FormMain_Load(object sender, EventArgs e)
{
KeyDown += FormMain_KeyDown;
}
Run Code Online (Sandbox Code Playgroud)
和被调用的函数:
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
// Do something.
}
}
Run Code Online (Sandbox Code Playgroud)
问题是表单不会对Escape(或我试过的任何其他键)做出反应.有趣的是,同一个项目的不同形式使用类似的代码而没有任何问题.谁能告诉我我做错了什么?
我有以下方法:
public static List<string> GetArgsListStartsWith(string filter, bool invertSelection, bool lowercaseArgs)
{
return GetArgumentsList(lowercaseArgs)
.Where(x => !invertSelection && x.StartsWith(filter)).ToList();
}
Run Code Online (Sandbox Code Playgroud)
然后我就这样称呼它 GetArgsListStartsWith("/", true, false)
这将转化为:获取所有不以"/"开头的参数列表.问题是列表没有填充,即使所有参数都不以"/"开头.
如果我调用GetArgsListStartsWith("/", false, false)
哪个转换为:获取以"/"开头的所有参数的列表,则列表将填充以"/"开头的参数.
我怀疑当设置为true并返回false 时!invertSelection && x.StartsWith(filter)
不会返回,但我不明白为什么.有人看到我不喜欢的东西吗?true
invertSelection
x.StartsWith(filter)
我是否必须担心删除分配给局部变量的事件侦听器?
考虑以下示例:
var zipUtil = new ZipUtil();
zipUtil.ProgressChanged += ZipUtil_ProgressChanged;
Run Code Online (Sandbox Code Playgroud)
我正在创建一个ZipUtil
类的实例,该实例存储为方法中的局部变量。我是否必须zipUtil.ProgressChanged -= ZipUtil_ProgressChanged;
在方法终止之前删除侦听器 ( ) 或者跳过该步骤是否可以?
c# ×10
mainwindow ×2
winforms ×2
wpf ×2
class ×1
controls ×1
discord ×1
discord.net ×1
events ×1
icons ×1
if-statement ×1
keydown ×1
linq ×1
namespaces ×1
repeat ×1
resources ×1
securestring ×1
variables ×1
wcf ×1
where ×1
xaml ×1