2011-09-15 38 views
1

VS2008(框架3.5)上的某些東西似乎無法在VS2010(框架4)上運行。在VS2008上運行的運行時更改窗口樣式,在VS2010上無法運行

我需要在運行時更改窗口的樣式(用戶首選項)。 在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; 
    } 

    } 
} 

當我運行框架3.5這個代碼顯示大小爲200×200的窗口。 當我運行框架4 THW窗口這個代碼顯示大小爲500x400

最奇怪的是,如果我添加MinWidth和了minHeight在運行時應用該樣式的屬性正常工作也是在VS2010,而寬度和高度似乎被忽略。

有人有解決這個問題的辦法嗎?

回答

0

AFAIK更改如何顯示的窗口和/或它的大小,你應該只執行ArrangeOverride或者設置HeightWidth直接(例如在Loaded事件handlet)...

編輯 - 按評論:

在運行時應用XAML可以開闢一些安全問題,所以我建議不這樣做,或者至少實現一些安全措施,防止動態加載的XAML從與應用......這說搞亂:

This MS Connect entry是不完全一樣的問題,但這樣mehow相關並建議可能存在WPF 4中的錯誤。

+0

設置高度和寬度不是解決方案。在我的程序中,樣式存儲爲一種「用戶選項」,並從xml文件加載。我將看看ArrangeOverride,但我真正需要的是在運行時應用樣式。 –

+0

雖然我不確定「注入」一些XML是不錯的選擇,但您可以在runme上應用樣式... – Yahia

+0

確定它有效,但它不是我所需要的。這個「用戶選項」(它比這更復雜,但將其視爲用戶選項)可以包含任何有效的樣式設置器,不僅包括寬度和高度。 –