class SomeClass
{
private struct PhraseInfo
{
public int Start;
public int Length;
}
...
private void SomeMethod(...)
{
List<PhraseInfo> posesBracket = new List<PhraseInfo>();
posesBracket.Add(new PhraseInfo());
posesBracket[0].Start = 10;
}
Run Code Online (Sandbox Code Playgroud)
of cause,posesBracket [0] .start = 10; 发生编译器错误CS1612:"无法修改'表达式'的返回值,因为它不是变量"
我怎样才能修改列表中的值?
这是我的序列化/反序列化的类.
public class MyDic
{
...
[XmlElement(IsNullable = true)]
public List<WebDefinition> WebDefinitions;
...
}
Run Code Online (Sandbox Code Playgroud)
它是结构的完整定义WebDefinition.
public struct WebDefinition
{
public string Definition;
public string URL;
public WebDefinition(string def, string url)
{
Definition = def;
URL = url;
}
public override string ToString() { return this.Definition; }
}
Run Code Online (Sandbox Code Playgroud)
我希望Dictionary.WebDefinitions在反序列化时可以为空.但是当它发生运行时错误
//runtime error : System.InvalidOperationException
XmlSerializer myXml = new XmlSerializer(typeof(Dictionary), "UOC");
Run Code Online (Sandbox Code Playgroud)
为什么我不能用XmlElementAttribute.IsNullable?
注1:
当我删除一行时[XmlElement(IsNullable = true)],它正常工作,没有错误.(序列化和反序列化).
note2:
异常是System.InvalidOperationException,消息是:"error occured in during reflection …
请先看下面的代码.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
public struct MyStruct
{
public List<MyStructItem> Items;
}
public struct MyStructItem
{
public string Value;
}
static void Main(string[] args)
{
List<MyStruct> myList = new List<MyStruct>();
myList.Add(new MyStruct());
//(!) it haven't comipled.
if (myList[0].Items = null){Console.WriteLine("null!");}
//(!) but it have compiled.
if (myList[0].Items != null) { Console.WriteLine("not null!"); }
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么区别!=null,并=null在这种情况下?
谢谢.
这是我的资源键枚举。
public enum UOCResKeys
{
DicView_FontFamily = 10000,
DicView_BaseFontSize,
DicView_TitleFontSize,
DicView_TitleFontWeight,
DicView_SubtitleFontSize,
DicView_SubtitleForeGround,
}
Run Code Online (Sandbox Code Playgroud)
以下代码通过键引用资源。
ResourceDictionary appRes = Application.Current.Resources;
appRes[UOCResKeys.DicView_FontFamily] = new FontFamily(set.FontFamily);
Run Code Online (Sandbox Code Playgroud)
像这样,我如何引用xaml中枚举定义的资源键?
我们可以使用控制模板轻松制作图标按钮,如下面的代码:
<Style x:Key="IconButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Image x:Name="Background" Source="/UOC;component/TOOLBAR_BUTTON_NORMAL.png"/>
<Image Source="/UOC;component/ICON_SLICER.gif" Width="20" Height="20" Margin="0,-10,0,0"/>
<TextBlock Foreground="White" FontSize="9" Text="{TemplateBinding Button.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,15,0,0"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Source" TargetName="Background" Value="/UOC;component/TOOLBAR_BUTTON_OVER.png"/>
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Source" TargetName="Background" Value="/UOC;component/TOOLBAR_BUTTON_CLICK.png"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
但我认为这在实践中并不是一种富有成效的方式.因为我无法为每个图标按钮制作多种样式.(例如,我们假设App中有三个按钮:'打开'按钮,'关闭'按钮和'导航'按钮.这些按钮有不同的图标集.我不能制作像'IconButton_Close','IconButton_Open','IconButton_Nav'这样的样式这太愚蠢了.)
UserControl可能是一个答案.但我认为这不是一个聪明的方法.因为如果我创建UserControl,它将只是Button控件的包装器.这不是一个正确的方法.
所以,给我一个图标按钮的最佳方式.
谢谢.
当我要订阅活动时,我就是这样编码:(在visual studio 2010中)
this.Loaded +=
Run Code Online (Sandbox Code Playgroud)
this.Loaded+=new RoutedEventHandler(someClass_Loaded);
Run Code Online (Sandbox Code Playgroud)
private void someClass_Loaded()
{
}
Run Code Online (Sandbox Code Playgroud)
namespace System.Windows
{
[...]public delegate void RoutedEventHandler(object sender, RoutedEventArgs e);
}
Run Code Online (Sandbox Code Playgroud)
private void someClass_Loaded(object sender, RoutedEventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)
这是最好的编码练习吗?
PS.当我通过匿名方法订阅事件时,我确实喜欢这样.