【问题标题】:Compile WPF Xaml using Add-type of powershell without using PowerBoots使用 Add-type of powershell 编译 WPF Xaml 而不使用 PowerBoots
【发布时间】:2011-08-17 07:39:34
【问题描述】:

我想添加嵌入在 XAML 中的 WPF 代码隐藏并使用 powershell add-type 进行编译。 有 PowerBoots 但我不想使用它。我要嵌入的代码是 here 。我提到了The PowershellGuy way 的实现,但我仍然需要使用文件后面的代码。

Add-Type -AssemblyName presentationframework
[xml]$xaml = @' 
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1"  Height="350" Width="525" AllowsTransparency="True"  
WindowStyle="None" 
WindowState="Maximized" Topmost="False"  IsHitTestVisible="False">
</Window> 
'@ 

$reader=(New-Object Xml.XmlNodeReader $xaml) 
$Form=[Windows.Markup.XamlReader]::Load( $reader ) 
$Form.ShowDialog() | out-null

链接中代码背后的代码是

protected override void OnRender(DrawingContext drawingContext)
{ 
   base.OnRender(drawingContext); 
   var screenGeometry = new RectangleGeometry(new Rect(0, 0, ActualWidth,   ActualHeight)); 
   var excludeRectangle = new RectangleGeometry(new Rect(200, 200, 150, 150)); 
   drawingContext.PushClip(CombinedGeometry.Combine(screenGeometry, excludeRectangle, GeometryCombineMode.Exclude, null)); 
   drawingContext.PushOpacity(.8);
   drawingContext.DrawRectangle(Brushes.Black, null, new Rect(0, 0, ActualWidth, ActualHeight));
   drawingContext.Pop(); drawingContext.Pop(); 
    } 

谢谢!

【问题讨论】:

    标签: wpf xaml powershell inline add-type


    【解决方案1】:

    我不知道它是否有帮助,但这里有一个示例,其中在按钮“单击”操作后面添加了某种 PowerShell 代码。

    #requires -version 2
    Add-Type -AssemblyName PresentationFramework
    [xml]$xaml = 
    @"
    <Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="408">
        <Grid>
          <Button x:Name="button1"
                    Width="75"
                    Height="23"
                    Canvas.Left="118"
                    Canvas.Top="10"
                    Content="Click Here" />
        </Grid>
    </Window>
    "@
    
    Clear-Host
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $target=[Windows.Markup.XamlReader]::Load($reader)
    $control=$target.FindName("button1")
    $eventMethod=$control.add_click
    $eventMethod.Invoke({$target.Title="Hello $((Get-Date).ToString('G'))"})
    $target.ShowDialog() | out-null 
    

    【讨论】:

    • 听起来不错。我认为嵌入没有 XAML 的 C# 代码比 Xaml 本身更可取。感谢 JPBlanc 和 JayKul!
    【解决方案2】:

    您不能在通过 XamlReader 加载的 XAML 上进行代码隐藏,这基本上意味着您无法执行您正在尝试执行的操作(在松散的 XAML 中覆盖 Window 的 OnRender 方法)从 Window 类派生的新类型 ...

    但我认为这对于你想要做的事情来说太过分了:

    Add-Type -AssemblyName presentationframework
    [xml]$xaml = @' 
    <Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    AllowsTransparency="True" WindowStyle="None" 
    Background="#AA000000" WindowState="Maximized" 
    Height="{x:Static SystemParameters.PrimaryScreenHeight}"
    Width="{x:Static SystemParameters.PrimaryScreenWidth}" 
    Title="Window Title" Topmost="False"  IsHitTestVisible="False"></Window> 
    '@ 
    
    $reader=(New-Object Xml.XmlNodeReader $xaml) 
    $Form=[Windows.Markup.XamlReader]::Load( $reader ) 
    $Form.ShowDialog() | out-null
    

    【讨论】:

      猜你喜欢
      • 2012-03-31
      • 2014-01-21
      • 1970-01-01
      • 2010-10-23
      • 2021-03-04
      • 2011-04-15
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多