简体   繁体   English

C#XAML-将导航按钮添加到浏览器工具栏

[英]C# XAML- adding navigation buttons to a browser toolbar

I have added a web browser to my C# application using the following XAML code: 我已使用以下XAML代码将Web浏览器添加到C#应用程序中:

<WebBrowser Name="rivBrowser" Height="550" Width="620" Margin="0, 40, 0, 0" Visibility="Visible" />
    <DockPanel>
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar Header="File">
                <Button Command="New" Content="New" ToolBar.OverflowMode="Always" />
                <Button Command="Open" Content="Open" ToolBar.OverflowMode="Always" />
                <Button Command="Save" Content="Save" ToolBar.OverflowMode="Always" />
            </ToolBar>
            <ToolBar Header="Edit" Margin="5.4,0,-5.4,0">
                <Button Command="Cut" Content="Cut" ToolBar.OverflowMode="Always" />
                <Button Command="Copy" Content="Copy" ToolBar.OverflowMode="Always" />
                <Button Command="Paste" Content="Paste" ToolBar.OverflowMode="Always" />
            </ToolBar>
            <ToolBar Margin="9.2,0,-8.2,0">
                <Button Command="Back" ToolTip="Return to the previous page"/>
                <Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateBack.png" Width="20" Height="20" Margin="0,0,0,2.4" />
            </ToolBar>
            <ToolBar Margin="16.4,0,-16.2,0" >
                <Button Command="Forward" ToolTip="Proceed to the next page" />
                <Image Source="C:\Users\elgan\workspace\browser\riviam_windows\Images\navigateForward.png" Width="20" Height="20" />
            </ToolBar>
        </ToolBarTray>



    </DockPanel>

However, I'm getting some exceptions with the 'Forward' and 'Back' navigation buttons that I'm trying to add to the toolbar. 但是,我尝试添加到工具栏的“前进”和“后退”导航按钮遇到了一些例外。 The exceptions say: 例外情况如下:

Exception thrown 抛出异常

'System.Windows.Markup.XamlParseException' in PresentationFramework.dll PresentationFramework.dll中的'System.Windows.Markup.XamlParseException'

and

Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' 附加信息:“在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供值”引发了异常。 Line number '73' and line position '22'. 行号“ 73”和行位置“ 22”。

That line is the line <Button Command="Back" ToolTip="Return to the previous page"/> 该行是<Button Command="Back" ToolTip="Return to the previous page"/>

It's the button I want to use to allow the user to navigate back to the previous page displayed in the browser- but I'm not sure that I'm doing this correctly... Should I be calling a method from my C# code here? 这是我要用来允许用户导航回到浏览器中显示的上一页的按钮,但是我不确定我是否正确执行了此操作……我应该在这里从C#代码中调用方法吗? ? How do I do that? 我怎么做?

I've not used C# in about 6 years, so am not very familiar with it or how it works... 我大约6年没有使用C#了,所以对它或它的工作方式不是很熟悉。

Since you don't say if you are using the MVVM pattern or not I'll assume you don't. 由于您没有说是否正在使用MVVM模式,所以我假设您没有。

In this case, you'll want to subscribe to the Click event of the Button rather than using a Command 在这种情况下,您将要订阅ButtonClick事件,而不是使用Command

For example : 例如 :

<Button Click="GoForward_OnClick" ToolTip="Proceed to the next page" />

And on your code-behind (yourfile.xaml.cs) you'll have the method called when the button is clicked : 在代码隐藏(yourfile.xaml.cs)上,您将具有单击按钮时调用的方法:

private void GoForward_OnClick(object sender, RoutedEventArgs e)
{
    // Your code logic goes here
}

Your Command should be pointing to a DelegateCommand object on your viewmodel. 您的命令应该指向视图模型上的DelegateCommand对象。 Here is a sample: 这是一个示例:

View: 视图:

<Button Command="{Binding SaveCommand}" />

ViewModel: ViewModel:

public DelegateCommand SaveCommand { get; private set; }

public SampleViewModel()
{
    SaveCommand = new DelegateCommand(Save);
}

private void Save()
{
    // do something
}

If you want to use built in ApplicationCommands the you should use such syntax of Command like ApplicationCommands.Cut . 如果要使用内置的ApplicationCommands,则应使用Command语法,例如ApplicationCommands.Cut

For example: 例如:

<Button Content="Cut" Command="ApplicationCommands.Cut"/>
<Button Content="Copy"  Command="ApplicationCommands.Copy"/>
<Button Content="Paste" Command="ApplicationCommands.Paste"/>

MSDN has a good article about a list of available Application Commands : MSDN上有一篇很好的文章,介绍了可用的应用程序命令列表:

CancelPrint - Gets the value that represents the Cancel Print command. CancelPrint-获取代表“取消打印”命令的值。 System_CAPS_pubpropertySystem_CAPS_static System_CAPS_pubpropertySystem_CAPS_static

Close - Gets the value that represents the Close command. Close-获取代表“关闭”命令的值。

ContextMenu - Gets the value that represents the Context Menu command. ContextMenu-获取代表Context Menu命令的值。

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM