我需要在C#工作的人的帮助。我试图实现在URL上打开Internet Explorer的WPF应用程序,我将其作为参数补充。所以在完美的世界里,我想运行这样的东西:
\\path\window.exe X x Y "URL" "Title"
其中X,Y是窗口的高度和宽度,URL和Title是该窗口的标题。我对C#的了解是不存在的,所以在广泛使用google之后我设法获得了这个XAML:
<Window x:Class="WPFWebControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My Web Browser" WindowState="Normal" Loaded="Window_Loaded" WindowStyle="ThreeDBorderWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="Auto" Width="Auto">
<Grid>
<WebBrowser Height="Auto" HorizontalAlignment="Left" Margin="0,28,0,0" Name="MyWebBrowser" VerticalAlignment="Top" Width="Auto" LoadCompleted="MyWebBrowser_LoadCompleted" />
<TextBox Height="23" Margin="40,5,0,0" Name="MyTextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="708" />
<Button Content="Go" Margin="10,5,0,476" Name="MyGo" ToolTip="Go" Click="MyGo_Click" RenderTransformOrigin="-0.76,0.565" HorizontalAlignment="Left" Width="25" />
</Grid>
</Window>
和这个C#代码:
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;
namespace WPFWebControl
{ /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
MyWebBrowser.Source = new Uri("http://www.google.com");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void MyGo_Click(object sender, RoutedEventArgs e)
{
try
{
MyWebBrowser.Source = new Uri("http://" + MyTextBox.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void MyWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{}
}
}
现在我需要一些建议如何通过参数设置窗口大小,网址和标题,这样我就可以摆脱那个地址文本框和按钮。
答案 0 :(得分:1)
您可以打开一个新窗口并将WebBrowser
设置为Content
,例如:
WebBrowser browser = new WebBrowser();
Window win = new Window();
win.Content = browser;
win.Height = 100;
win.Width = 100;
win.Show();
win.Title = "title...";
browser.Navigate("http://google.com");
如果您希望应用程序能够接受命令行参数,则可以从StartupUri
中删除App.xaml
属性并覆盖OnStartup
App.xaml.cs
方法:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
try
{
double width = Convert.ToDouble(e.Args[0]);
double height = Convert.ToDouble(e.Args[1]);
string title = e.Args[2];
string url = e.Args[3];
WebBrowser browser = new WebBrowser();
Window win = new Window();
win.Content = browser;
win.Height = height;
win.Width = width;
win.Show();
win.Title = title;
browser.Navigate(new Uri(url, UriKind.Absolute));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
<强>用法:强>
WpfApplication1.exe 100 100 "title..." "http://google.com"
答案 1 :(得分:0)
你应该可以使用
string[] args = Environment.GetCommandLineArgs();
或者,在App.xaml中更改启动uri(删除它),然后创建自己的启动,如
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
//Manually create and show your window, passing the arguments
}
}
等