在VS2008上运行的运行时更改窗口样式,在VS2010上不起作用

时间:2011-09-15 09:42:08

标签: c# wpf visual-studio-2010 xaml

在VS2008(框架3.5)上运行的东西似乎不适用于VS2010(框架4)。

我需要在运行时更改窗口的样式(用户首选项)。 在VS2008中,此代码正在运行:

Window1.xaml

<Window x:Class="StyleTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        ResizeMode="CanResizeWithGrip">

  <Window.Style>
    <Style TargetType="{x:Type Window}">
      <Setter Property="MinWidth" Value="400" />
      <Setter Property="Width" Value="500" />
      <Setter Property="MinHeight" Value="400" />
      <Setter Property="SizeToContent" Value="Height" />
    </Style>
  </Window.Style>

  <Grid>

  </Grid>
</Window>

Window1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Windows.Markup;
using System.IO;

namespace StyleTest
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();

      Loaded += new RoutedEventHandler(ObjDialog_Loaded);
    }

    void ObjDialog_Loaded(object sender, RoutedEventArgs e)
    {
      XmlDocumentFragment frag = new XmlDocument().CreateDocumentFragment();
      frag.InnerXml = "<Style TargetType=\"{x:Type Window}\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"> " +
                      "  <Setter Property=\"Height\" Value=\"200\" />" +
                      "  <Setter Property=\"Width\" Value=\"200\" />" +
                      "</Style>";
      XmlNode node = frag.FirstChild as XmlElement;

      Style style = LoadXaml(node.OuterXml) as Style;
      if (style != null)
        Style = style;

      UpdateLayout();
    }

    private object LoadXaml(string xaml)
    {
      Exception ex = null;
      object o = LoadXaml(xaml, out ex);

      if (ex != null)
        throw ex;

      return o;
    }

    public static object LoadXaml(string xaml, out Exception exception)
    {
      try {
        ParserContext pc = new ParserContext();
        pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        pc.XmlnsDictionary.Add("l", "http://www.teradp.com/schemas/GN4/1/WinUI");
        pc.XmlnsDictionary.Add("c", "clr-namespace:TeraDP.GN4.Common;assembly=Common");

        exception = null;
        return XamlReader.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), pc);
      }
      catch (Exception ex) {
        exception = ex;
      }

      return null;
    }

  }
}

当我在Framework 3.5上运行此代码时,窗口显示的大小为200x200。 当我在Framework 4上运行此代码时,显示的窗口大小为500x400

最奇怪的是,如果我将MinWidth和MinHeight添加到运行时应用的样式,那么这些属性也可以在VS2010中正常工作,而宽度和高度似乎被忽略。

有人有解决此问题的方法吗?

1 个答案:

答案 0 :(得分:0)

AFAIK更改窗口的显示方式和/或其大小,您应该直接实施ArrangeOverride或直接设置HeightWidth(例如在Loaded事件中handlet)...

编辑 - 根据评论:

在运行时应用XAML可能会打开几个安全问题,所以我建议不要这样做,或者至少实施一些安全措施来防止动态加载的XAML搞乱应用程序......说:

This MS Connect entry不是完全相同的问题,但不知何故相关并暗示WPF 4中可能存在错误。