我正在嘗試為我不久前撰寫的腳本創建一個 GUI。我遇到了一些麻煩。我想讓“提交條目”按鈕記錄用戶輸入的資料并將其分配給變數($fromcompname,$tocompname,$userid)我不確定我是否需要一個函式來提取輸入文本框并將它們分配給這些變數或使它起作用的東西。
表單本身是在我在 VS Pro 中制作的 xaml 中,然后我將它移動到 vs 代碼,因為我正在使用 powershell 運行這個腳本。視窗出現所有元素我只是不確定如何獲取“提交條目”按鈕來記錄用戶輸入的資料并將其傳遞給腳本的其余部分以包含“傳輸資料”按鈕。如果您有任何問題需要問我以更好地理解意圖,請做,我希望能找到答案。謝謝你。
下面的代碼:
Add-Type -AssemblyName PresentationFramework
#Builds the window/form for the GUI
[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="Profile Transfer" WindowStartupLocation = "CenterScreen"
Width = "460" Height = "220" ShowInTaskbar = "True">
<Grid Margin="0,0,0,0" Background="#FF3D0198">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="17*"/>
<ColumnDefinition Width="443*"/>
</Grid.ColumnDefinitions>
<Button x:Name="subbttn" Content="Submit Entries" HorizontalAlignment="Left" Margin="290,84,0,0" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="18"/>
<Button x:Name="transbttn" Content="TRANSFER Data" HorizontalAlignment="Left" Margin="145,124,0,0" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="18"/>
<TextBox HorizontalAlignment="Left" Margin="290,15,0,0" TextWrapping="Wrap" Text="Old PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
<TextBox HorizontalAlignment="Left" Margin="290,38,0,0" TextWrapping="Wrap" Text="New PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
<TextBox HorizontalAlignment="Left" Margin="290,61,0,0" TextWrapping="Wrap" Text="User ID" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
<Label x:Name="fromlbl" Content="What is the name of the Old PC?" HorizontalAlignment="Left" Margin="1,11,0,0" VerticalAlignment="Top" Width="202" Foreground="White" FontFamily="Times New Roman" Grid.Column="1" Height="24"/>
<Label x:Name="tolbl"Content="What is the name of the New PC?" HorizontalAlignment="Left" Margin="1,34,0,0" VerticalAlignment="Top" Foreground="White" FontFamily="Times New Roman" Grid.Column="1" Height="24" Width="173"/>
<Label x:Name="uidlbl"Content="What is the USER's ID?" HorizontalAlignment="Left" Margin="1,57,0,0" VerticalAlignment="Top" Foreground="White" FontFamily="Times New Roman" Grid.Column="1" Height="24" Width="126"/>
<StatusBar Grid.Column="1" Margin="0,147,33,10"/>
</Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
#Declares the variables based on the Name input in the XAML elements
$subbttn = $window.FindName("subbttn")
$transbttn = $window.FindName("transbttn")
$fromlabel = $window.FindName("fromlbl")
$tolabel = $window.FindName("tolbl")
$useridlabel = $window.FindName("uidlbl")
#Declares the needed data based on the user's input in the Textboxes
$subbttn.Add_Click({
$fromcompname = $fromlabel.Text
$tocompname = $tolabel.Text
$userid = $useridlabel.Text
Write-Host "$fromcompname" "$tocompname" "$userid"
})
#Opens the path to the old PC and the new PC for Robocopy
Set-Item -path env:computername -Value "$fromcompname"
Set-Item -path env:computername -Value "$tocompname"
#Sets the clikc of the transfer data button to initiate the robocopy
$transbttn.Add_Click({
#robocopy is more intelligent
C:\windows\system32\Robocopy.exe "\\$fromcompname\c$\Users\$userid" "\\$tocompname\c$\Users\$userid" `
"/XJ" "/MIR" "/PURGE" `
"/R:2" "/W:5" "/XA:SH" "/S" "/256" "/XF" "*.MP3" "desktop.ini" `
"/XD" "AppData" "Contacts" "Downloads" "Links" "Music" "SapWorkDir" "Searches" "Tracing" "Remote Assistance Logs" "Saved Games"
#ROBOCOPY NOTES
#/XJ excludes Junction Directories, breaks the "Application Data" loop Robocopy has a bug with
#/MIR Mirrors a directory tree
#/PURGE Deletes destination files and directories that no longer exist in the source.
#/S Copies subdirectories and excludes empty directories.
#/R:0 Specifies the number of retries on failed copies
#/W:0 Specifies the wait time between retries in seconds
#/XF Excludes Files
#/XD Excludes Directories
#/COPY:DATSOU copies security permissions of folders
})
$window.ShowDialog()
uj5u.com熱心網友回復:
正如TheMadTechnician評論的那樣,您需要在 gui 中為 TextBoxes 設定變數,而不是 Labels。
為此,您必須為這些文本框命名,例如:
<TextBox x:Name="fromcompany" HorizontalAlignment="Left" Margin="290,15,0,0" TextWrapping="Wrap" Text="Old PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
<TextBox x:Name="tocompany" HorizontalAlignment="Left" Margin="290,38,0,0" TextWrapping="Wrap" Text="New PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
<TextBox x:Name="userid" HorizontalAlignment="Left" Margin="290,61,0,0" TextWrapping="Wrap" Text="User ID" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
然后創建變數以便能夠參考它們
$fromcompanyTB = $window.FindName("fromcompany")
$tocompanyTB = $window.FindName("tocompany")
$useridTB = $window.FindName("userid")
在 Click() 事件處理程式中,您需要腳本范圍才能使用這些變數,否則它們將只是腳本塊本地的變數:
$subbttn.Add_Click({
$script:fromcompname = $script:fromcompanyTB.Text
$script:tocompname = $script:tocompanyTB.Text
$script:userid = $script:useridlabelTB.Text
Write-Host "$script:fromcompname $script:tocompname $script:userid"
})
這同樣適用于$transbttn.Add_Click({})
最后,需要在Content=最后兩<Label行前面加一個空格,否則Xaml無效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/528447.html
