小编Til*_*lak的帖子

此操作将创建结构不正确的文档

我是新手,XML并尝试了以下但我得到了一个例外.有人能帮我吗?

例外是 This operation would create an incorrectly structured document

我的代码:

string strPath = Server.MapPath("sample.xml");
XDocument doc;
if (!System.IO.File.Exists(strPath))
{
    doc = new XDocument(
        new XElement("Employees",
            new XElement("Employee",
                new XAttribute("id", 1),
                    new XElement("EmpName", "XYZ"))),
        new XElement("Departments",
            new XElement("Department",
                new XAttribute("id", 1),
                    new XElement("DeptName", "CS"))));

    doc.Save(strPath);
}
Run Code Online (Sandbox Code Playgroud)

.net c# linq asp.net linq-to-xml

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

Directory.GetFiles找到不存在的文件

我只是偶然发现了这些GetFiles方法的无证行为System.IO.Directory.

每当searchPattern传递给方法的参数包含保留的Windows设备名称(例如"nul.*"或)时"aux.bmp",该方法返回一个包含不存在的文件名称的数组,如C:\Users\ft1\nulD:\aux等.

我想知道那些设备名称是否具有特殊含义的上下文,如"." 或"..",或者如果这只是一种错误.无论如何,这看起来仍然很奇怪.例如,C#中的这段代码片段:

string[] fileNames = Directory.GetFiles(@"C:\D:\..\..\...\", "con.txt");
foreach (string fileName in fileNames) Console.WriteLine(fileName);
Run Code Online (Sandbox Code Playgroud)

版画

C:\D:\..\..\...\con
Run Code Online (Sandbox Code Playgroud)

有线索吗?

.net c# windows file getfiles

19
推荐指数
2
解决办法
937
查看次数

装饰设计模式与访客设计模式的区别

我相信了解Decorator和Visitor设计模式的意图.

虽然我可以列出以下差异

  1. Decorator在一个对象上工作,访问者在复合结构上工作,
  2. 装饰者是结构设计模式,访客是行为设计​​模式.

当我深入思考时,我无法说服自己两者之间的真正区别.

design-patterns decorator visitor

17
推荐指数
5
解决办法
9217
查看次数

如何在C#中将System.Array转换为object []

我有一个COM函数,期望object[]作为参数:

foo(object[] values)
Run Code Online (Sandbox Code Playgroud)

我想传递一些enum字段,所以我使用以下内容:

object[] fields = (object[])Enum.GetValues(typeof(SomeEnumType));
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试传递fieldsfoo(...)ie [ foo(fields)]时,我收到一个错误:

"无法将类型为'SomeEnumType []'的对象强制转换为'system.Object []'.

谁能告诉我我做错了什么?

.net c# enums casting c#-4.0

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

动态更改数据模板

我有一个绑定到任务的项控件.每个任务都有任务状态.我为每个任务状态定义了不同的数据模板,还为数据模板选择器定义

问题是,当动态更改任务状态时,我无法弄清楚如何触发数据模板选择器.

我想知道如何将数据触发器与数据模板一起使用.

如果这不会成功,我将探索其他替代方案,例如
1.附加属性绑定到任务状态.任何更改都将动态设置数据模板.
2.视觉状态管理器

wpf triggers datatemplate datatemplateselector

15
推荐指数
2
解决办法
2万
查看次数

基于不同XAML的样式继承

如何将样式中的BasedOn标记指定为某些其他文件中定义的样式.

例,

Dictionary1.xaml定义

   <Style x:Key="basicStyle" TargetType="TextBlock" >
       <Setter Property="FontSize" Value="24"></Setter>
       <Setter Property="Foreground" Value="DarkGray"></Setter>
       <Setter Property="FontWeight" Value="Bold"></Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

在Dictionary2.xaml中我需要类似的东西

    <Style x:Key="headerStyle" TargetType="TextBlock" >
       <Setter Property="FontSize" Value="46"></Setter>
       <Setter Property="Foreground" Value="DarkGray"></Setter>
       <Setter Property="FontWeight" Value="Bold"></Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?

.net c# wpf xaml resourcedictionary

13
推荐指数
1
解决办法
8864
查看次数

我真的需要在集合上使用AsQueryable()吗?

示例代码:

List<Student> Students = new List<Student>()   
{   
    new Student(101, "Hugo", "Garcia", new List<int>() { 91, 88, 76, 93 }),  
    new Student(102, "Rick", "Adams", new List<int>() { 70, 73, 66, 90 }),  
    new Student(103, "Michael", "Tucker", new List<int>() { 73, 80, 75, 88 }),  
    new Student(104, "Fadi", "Fakhouri", new List<int>() { 82, 75, 66, 84 }),  
    new Student(105, "Peter", "Barrows", new List<int>() { 67, 78, 70, 82 })  
};

var query = from student in Students
            where student.Marks.AsQueryable().All(m => m > …
Run Code Online (Sandbox Code Playgroud)

.net c# linq list asqueryable

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

在WPF中,我可以在2个按钮之间共享相同的图像资源

我想在WPF中创建一个On/Off按钮,我想让它在用户点击它时改变它的外观(如果它是关闭,如果它关掉,则切换到打开)使用图像.我将要使用的图像添加到资源中:

 <Window.Resources>
    <Image x:Key="Off1" Source="/WPFApplication;component/Images/off_button.png" Height="30" Width="70" />
    <Image x:Key="On1" Source="/WPFApplication;component/Images/on_button.png" Height="30" Width="70"/>
 </Window.Resources>
Run Code Online (Sandbox Code Playgroud)

而事件代码是,"flag"是一个布尔局部变量,初始化为true:

 private void OnOff1Btn_Click(object sender, RoutedEventArgs e)
    {
        if (flag)
        {
            OnOff1Btn.Content = FindResource("Off1");
            flag = false;     
        }
        else
        {
            OnOff1Btn.Content = FindResource("On1");
            flag  = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我需要创建2个开/关按钮,它们的行为相同.当我尝试为第二个按钮使用相同的资源时,我得到了一个例外:

 Specified element is already the logical child of another element. Disconnect it first.
Run Code Online (Sandbox Code Playgroud)

我可以在第二个按钮中使用相同的图像资源,还是必须将图像作为具有不同密钥的资源再次添加?

c# wpf xaml bitmapsource resourcedictionary

11
推荐指数
2
解决办法
7822
查看次数

如何在Windows 8商店应用程序中访问Parent的DataContext

这是基于XAML的应用程序(WPF/Silverlight/WinRT)中的常见情况.

WPF相关链接 - > WPF数据绑定:如何访问"父"数据上下文?

RelativeSourceAncestorType,并且Mode=FindAncestor通常在WPF中进行救援.

WinRT API中缺少这两个.如何访问父母(可能不是直接的)DataContext,?

(我知道TemplateBinding,ElementBinding但两者都不适用于DataTemplate).

wpf xaml windows-8 windows-runtime winrt-xaml

11
推荐指数
1
解决办法
7425
查看次数

从任何线程中捕获未处理的异常

编辑

这个问题的答案在哪里有用,谢谢我感谢帮助:)但我最终使用:http://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b#content


原始问题:

我想在应用程序崩溃时显示错误消息.

目前我有:

App.xaml:

<Application x:Class="WpfApplication5.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      DispatcherUnhandledException="App_DispatcherUnhandledException"  // <----------------
      StartupUri="MainWindow.xaml">
    <Application.Resources>             
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

代码隐藏App.xaml:

namespace WpfApplication5
{        
    public partial class App : Application
    {
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show("Caught Unhandled Exception!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当主线程发生错误时,该解决方案很有效.现在我的问题是如何能够捕获在不同线程上发生的错误?

换句话说:当我按下此按钮时,我能够捕获异常:( App_DispatcherUnhandledException被调用!)

    private void button1_Click(object sender, RoutedEventArgs e)
    {            
        int.Parse("lkjdsf");
    }
Run Code Online (Sandbox Code Playgroud)

但是我无法捕捉到这个异常:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Task.Factory.StartNew(() =>
        {
            int.Parse("lkjdsf");
        });
    }
Run Code Online (Sandbox Code Playgroud)

无论它们是否发生在主线程上,我如何能够捕获所有异常?

.net c# wpf exception unhandled-exception

10
推荐指数
1
解决办法
1万
查看次数