小编mjk*_*026的帖子

如何修改list <t>中的值?

    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:"无法修改'表达式'的返回值,因为它不是变量"

我怎样才能修改列表中的值?

c#

3
推荐指数
1
解决办法
3832
查看次数

我不能在序列化/反序列化中使用XmlElementAttribute.IsNullable

这是我的序列化/反序列化的类.

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 …

c# xml serialization

2
推荐指数
1
解决办法
2374
查看次数

!= null vs. = null

请先看下面的代码.

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在这种情况下?

谢谢.

.net c# null

0
推荐指数
2
解决办法
868
查看次数

通过xaml中的枚举定义动态资源键

这是我的资源键枚举。

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中枚举定义的资源键?

c# wpf enums xaml

0
推荐指数
1
解决办法
2173
查看次数

WPF:给我一个最好的图标按钮方式

我们可以使用控制模板轻松制作图标按钮,如下面的代码:

<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控件的包装器.这不是一个正确的方法.

所以,给我一个图标按钮的最佳方式.

谢谢.

wpf icons templates button

0
推荐指数
1
解决办法
4055
查看次数

C#:在Visual Studio 2010中的委托/事件订阅中进行更高效的编码

当我要订阅活动时,我就是这样编码:(在visual studio 2010中)

1.我写下如下代码:

this.Loaded +=
Run Code Online (Sandbox Code Playgroud)

2.我按Tab键.

3. IDE自动填写以下代码:

this.Loaded+=new RoutedEventHandler(someClass_Loaded);
Run Code Online (Sandbox Code Playgroud)

但是someClass方法尚未存在.所以我写了一个方法,如下面的代码:

private void someClass_Loaded()
{
}
Run Code Online (Sandbox Code Playgroud)

但是某些类的签名尚未正确定义.所以我将光标设置在委托"RoutedEventHandler"上.

6.我按F12.然后IDE向我展示了RoutedEventHandler的定义.

namespace System.Windows
{
    [...]public delegate void RoutedEventHandler(object sender, RoutedEventArgs e);
}
Run Code Online (Sandbox Code Playgroud)

7.复制RoutedEventHandler的参数.

8.我将它粘贴到someClass_Loaded方法

private void someClass_Loaded(object sender, RoutedEventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)

这是最好的编码练习吗?

PS.当我通过匿名方法订阅事件时,我确实喜欢这样.

c# visual-studio-2010

0
推荐指数
1
解决办法
188
查看次数

标签 统计

c# ×5

wpf ×2

.net ×1

button ×1

enums ×1

icons ×1

null ×1

serialization ×1

templates ×1

visual-studio-2010 ×1

xaml ×1

xml ×1