Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何在Powershell的xaml中使用相对路径_Wpf_Xaml_Powershell - Fatal编程技术网

Wpf 如何在Powershell的xaml中使用相对路径

Wpf 如何在Powershell的xaml中使用相对路径,wpf,xaml,powershell,Wpf,Xaml,Powershell,我有一个powershell脚本读取xaml文件并显示它。 在xaml文件中使用图像时,如: <Image Source="D:\Test\MyPic.png" /> 我必须提供绝对路径。是否有允许使用相对路径的语法? 黑客解决方案:读取xaml并在powershell脚本中以动态方式将“D:\Test”替换为当前模块路径 添加示例以澄清问题: Add-Type -assemblyName PresentationFramework, PresentationCore,

我有一个powershell脚本读取xaml文件并显示它。 在xaml文件中使用图像时,如:

<Image Source="D:\Test\MyPic.png"  />  

我必须提供绝对路径。是否有允许使用相对路径的语法? 黑客解决方案:读取xaml并在powershell脚本中以动态方式将“D:\Test”替换为当前模块路径

添加示例以澄清问题:

Add-Type -assemblyName PresentationFramework, PresentationCore, WindowsBase

$syncHash = [hashtable]::Synchronized(@{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"         
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)          
$psCmd = [PowerShell]::Create().AddScript({   
    [xml]$xaml = @"
    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen">
        <Image Source="D:\Test\MyPic.png" />
    </Window>
"@ 
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
    $syncHash.Window.ShowDialog() | Out-Null
})
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
添加类型-assemblyName PresentationFramework、PresentationCore、WindowsBase
$syncHash=[hashtable]::已同步(@{})
$newRunspace=[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState=“STA”
$newRunspace.ThreadOptions=“ReuseThread”
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable(“syncHash”,$syncHash)
$psCmd=[PowerShell]::Create().AddScript({
[xml]$xaml=@”
"@ 
$reader=(新对象System.Xml.XmlNodeReader$xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::加载($reader)
$syncHash.Window.ShowDialog()| Out Null
})
$psCmd.Runspace=$newRunspace
$data=$psCmd.BeginInvoke()

请注意:xaml未嵌入。Powershell会动态读取它。

如果图片和脚本位于同一文件夹中,我认为您需要这样的内容:

$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$settingspath = $directorypath + '\MyPic.png'
<Image Source=$settingspath  /> 

您可以使用一行程序轻松确定脚本目录。也可以考虑使用<代码>连接路径< /Cord>CMDLET来组合路径:

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$myPicPath = Join-Path $scriptPath 'MyPic.png'

谢谢你的反馈。 我坚持这样的解决方法:

$imgSplash = $syncHash.Window.FindName('imgSplash')
$splashUri = New-Object System.Uri $imgSplash.Source
$absolutPath = ("{0}\{1}" -f "D:\WhatEverPathNecessary,($splashUri.Segments | Select-Object -Last 1))
$imgSplash.Source = $absolutPath 

这需要在$syncHash.Window.ShowDialog()之前实现。

这完全不起作用。我猜是因为它在一个powershell中执行。谢谢你的主意。这是我试图避免的变通方法。我添加了一个小例子来澄清我的问题谢谢你的想法。这是我试图避免的变通方法。我添加了一个小示例来澄清我的问题。您还可以将工作目录(Set Location)更改为脚本的位置,然后使用相对路径。然而,这将是一个黑客。我提供的解决方案是最干净的IMHO.NOP:如果我更改并在AddScript中添加设置位置“D:\Test\”,那么如果在使用png之前使用
Set Location(分割路径-parent$MyInvocation.MyCommand.Definition)
更改位置,则无法找到图像(如果MyPic.png位于powershell脚本旁边)
$imgSplash = $syncHash.Window.FindName('imgSplash')
$splashUri = New-Object System.Uri $imgSplash.Source
$absolutPath = ("{0}\{1}" -f "D:\WhatEverPathNecessary,($splashUri.Segments | Select-Object -Last 1))
$imgSplash.Source = $absolutPath