forked from cefsharp/CefSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
94 lines (74 loc) · 3.1 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
94 lines (74 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using CefSharp.Example;
using CefSharp.Wpf.Example.Controls;
using CefSharp.Wpf.Example.ViewModels;
namespace CefSharp.Wpf.Example
{
public partial class MainWindow : Window
{
private const string DefaultUrlForAddedTabs = "https://www.google.com";
public ObservableCollection<BrowserTabViewModel> BrowserTabs { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
BrowserTabs = new ObservableCollection<BrowserTabViewModel>();
CommandBindings.Add(new CommandBinding(ApplicationCommands.New, OpenNewTab));
CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, CloseTab));
CommandBindings.Add(new CommandBinding(CefSharpCommands.Exit, Exit));
CommandBindings.Add(new CommandBinding(CefSharpCommands.OpenTabBindingTest, OpenTabBindingTest));
Loaded += MainWindowLoaded;
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
Title += " - " + bitness;
}
private void CloseTab(object sender, ExecutedRoutedEventArgs e)
{
if (BrowserTabs.Count > 0)
{
//Obtain the original source element for this event
var originalSource = (FrameworkElement)e.OriginalSource;
BrowserTabViewModel browserViewModel = null;
if (originalSource is MainWindow)
{
browserViewModel = BrowserTabs[TabControl.SelectedIndex];
BrowserTabs.RemoveAt(TabControl.SelectedIndex);
}
else
{
//Remove the matching DataContext from the BrowserTabs collection
browserViewModel = (BrowserTabViewModel)originalSource.DataContext;
BrowserTabs.Remove(browserViewModel);
}
browserViewModel.WebBrowser.Dispose();
}
}
private void OpenNewTab(object sender, ExecutedRoutedEventArgs e)
{
CreateNewTab();
TabControl.SelectedIndex = TabControl.Items.Count - 1;
}
private void MainWindowLoaded(object sender, RoutedEventArgs e)
{
CreateNewTab(CefExample.DefaultUrl, true);
}
private void CreateNewTab(string url = DefaultUrlForAddedTabs, bool showSideBar = false)
{
BrowserTabs.Add(new BrowserTabViewModel(url) { ShowSidebar = showSideBar });
}
private void OpenTabBindingTest(object sender, ExecutedRoutedEventArgs e)
{
CreateNewTab(CefExample.BindingTestUrl, true);
TabControl.SelectedIndex = TabControl.Items.Count - 1;
}
private void Exit(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
}
}