2017-03-03 10 views
-1

このPowershell関数コードブロックでフォルダを選択しました。次に、XAMLファイルを参照する2つの変数(sourcePath、source)を追加しました。ユーザーが "OK"を押すと、選択したフォルダパスを別のリストボックス "$ sourcePath"に表示する必要があります。私はリストボックスを "$ objForm.SelectedPath"の後ろに "$ sourcePath.Write($ objForm.SelectedPath)"と呼んでみました。私は何か間違っているが、それを修正する方法を知らない。ありがとうございました。powershellとWPFを使用してリストボックスに選択したディレクトリパスを表示する方法

$XAML = @' 
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Choose Folder" Height="350" Width="500"> 
<StackPanel>  
<Button x:Name="choose" Content="choose" HorizontalAlignment="Left" 
Margin="42,108,0,0" VerticalAlignment="Top" Width="121" /> 
<ListBox x:Name="sourcePath" HorizontalAlignment="left" Height="45" 
Margin="42,120,0,0" VerticalAlignment="Top" Width="400"/> 
</StackPanel> 
</Window> 
'@ 

$sourcePath=$win.Find("sourcePath") 
$source=$win.Find("source") 
$source.Add_click({Select-FolderDialog}) 
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |   
Out-Null 
Function Select-FolderDialog 
{param([string]$Description="Select Folder",[string]$RootFolder="Desktop") 
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog 
$objForm.Rootfolder = $RootFolder 
$objForm.Description = $Description 
$Show = $objForm.ShowDialog() 
    If ($Show -eq "OK") 
    { 
    Return $objForm.SelectedPath 
    $sourcePath.Write($objForm.SelectedPath) 
    } 
    Else 
    { 
     Write-Error "Operation cancelled by user." 
    } 
    $b = Select-FolderDialog 
} 
+0

サンプルを修正してください。定義されていないオブジェクトを参照する –

+0

ありがとう、Frode F、PeterXX。私は要求された参照オブジェクトを追加しました。 PeterXXは、私が探していたソリューションを投稿しました。あなたがた両方に感謝します。大変感謝しています。 –

答えて

0

Return $objForm.SelectedPathを削除します。値をコンソールに戻し、関数を終了します。つまり、$sourcePath.Write($objForm.SelectedPath)が実行されることはありません。

returnキーワードは、関数、スクリプト、またはスクリプトブロック

出典終了しますabout_Return

0

をリストボックスの中に何かを置くための「書き込み」を使用してのような間違ったごく少数のマイナーな事がありました。

もちろん、Frode F.のように、戻り値は除外されています。関数が終了する前に関数を終了する以外は、関数内にreturn文は必要ありません。

ここでは、リストボックスとボタンが付いたウィンドウを表示し、選択したパスをそのリストボックスに挿入するスクリプトコードを少し修正したものです。

$XAML = @' 

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Folder-Browser" 
    Height="500" 
    Width="600" 
> 
<StackPanel> 
    <ListBox x:Name="sourcePath" Height="300" Width="320" Margin="10"/> 
    <Button x:Name="choose" Content="Choose Folder" Width="120" Height="40" Margin="10"/> 
</StackPanel> 
</Window> 
'@ 

$Win = [Windows.Markup.XamlReader]::Parse($XAML) 
$sourcePath = $Win.FindName("sourcePath") 
$button = $Win.FindName("choose") 
$button.Add_Click({Select-FolderDialog}) 

Add-Type -Assembly System.Windows.forms 

function Select-FolderDialog 
{ 
    param([String]$Description="Select Folder", 
     [String]$RootFolder="Desktop") 

    $objForm = New-Object System.Windows.Forms.FolderBrowserDialog 
    $objForm.Rootfolder = $RootFolder 
    $objForm.Description = $Description 
    $Show = $objForm.ShowDialog() 
    if ($Show -eq "OK") 
    { 
    $SourcePath.Items.Add($objForm.SelectedPath) 
    } 
} 

$Win.ShowDialog() 

スクリプトのPowerShell 4.0で実行され、私はすぐにWindowオブジェクトを返し解析方法をお勧めします上記の場合:

$Win = [Windows.Markup.XamlReader]::Parse($Xaml) 
+0

ありがとう、PeterXX。あなたは私が探していたものを投稿しました。それを評価しました。 –

関連する問題