2011-09-15 8 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; 
    } 

    } 
} 

私はFramework 3.5のウィンドウ上でこのコードは200×200のサイズで表示されている実行すると。 フレームワーク4でこのコードを実行すると、ウィンドウが500x400のサイズで表示されます

実行時に適用されるスタイルにMinWidthとMinHeightを追加すると、VS2010でも正しく動作します。高さは無視されるようです。

誰かがこの問題の解決策を持っていますか?

答えて

0

私の知る限りでは...ウィンドウが表示され、および/またはそのサイズはあなただけArrangeOverrideを実装OR(Loadedイベントhandlet中など)に直接HeightWidthを設定する方法を

EDITを変更する - コメントどおり

:言った私はそうではないお勧めしますか、少なくともアプリケーションをいじってから動的にロードされたXAMLを防ぐために、いくつかのセキュリティ対策を実施しますので、いくつかのセキュリティ上の問題を開くことができ、実行時にXAMLを適用

...

This MS Connect entryので、まったく同じ問題ではありませんが、 WPF 4にバグがある可能性があることを示唆しています。

+0

高さと幅の設定は解決策ではありません。私のプログラムでは、スタイルは「ユーザオプション」の一種として格納され、xmlファイルからロードされます。 ArrangeOverrideを見ていきますが、本当に必要なのは、実行時にスタイルを適用することです。 –

+0

いくつかのXMLを「注入する」ことが良い選択肢であるとは確信していませんが、あなたはrunmeでスタイルを適用できます。 – Yahia

+0

確かに動作しますが、必要なものではありません。この「ユーザーオプション」(これよりもはるかに複雑ですが、ユーザーオプションと見なします)には、幅と高さだけでなく、有効なスタイル設定ツールを含めることができます。 –

関連する問題