UWP Content Dialog and Content Frame - c#

I am having a little problems with my frame.
I write a static method, it draws a content dialog with a textbox inside, if the textbox isn't empty, it should open the page_2.
Currently I am using "ContentFrame.Navigate(typeof(Page_2), "operation");" ,
But my project have a pane Hamburguer menu on mainpage and it should be on all the pages.
and if I use ContentFrame.Navigate it disappears.
Here are my static content dialog:
public static async void D_NewProject(double Widthmax)
{
ContentDialog D_NewProject = new ContentDialog()
{
Title = "New Project",
MaxWidth = Widthmax, // this.ActualWidth,
};
StackPanel D_panel = new StackPanel();
TextBox TextBox_D1 = new TextBox()
{
PlaceholderText = "Name:",
TextWrapping = TextWrapping.Wrap,
};
D_panel.Children.Add(TextBox_D1);
D_NewProject.Content = D_panel;
D_NewProject.PrimaryButtonText = "OK";
D_NewProject.PrimaryButtonClick += delegate
{
if (TextBox_D1.Text != "")
{
/**/
Frame ContentFrame = Window.Current.Content as Frame;
ContentFrame.Navigate(typeof(Page_2), "operation");
/**/
}
};
await D_NewProject.ShowAsync();
}
If you need more code like the pane, you can ask for it.
Any help is appreciated.

If you want to have a layout page that would be active in all page,
you need to make a method in App.xaml.cs which loads the current page inside of this layout.
I can suggest you this method that I use it myself:
public static void LoadLayout(Type pageType)
{
// just ensure that the window is active
RootFrame = new _Layout();
RootFrame.ContentFrame.Navigate(pageType, null);
RootFrame.ContentFrame.Navigated += ContentFrame_Navigated;
ContentFrame_Navigated(null, null);
Window.Current.Content = RootFrame;
}
The ContentFrame_Navigated delegate is used just for controlling the items on the layout (for example: Buttons, ...).

Related

How to detect Xamarin Forms tabbed page click - iOS?

My scenario is, I have a Tabbed page in Xamarin Forms:
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
var playPage = new NavigationPage(new PlayPage())
{
Title = "Play",
Icon = "play.png"
};
var settingsPage = new NavigationPage(new SettingsPage())
{
Title = "Settings",
Icon = "settings.png"
};
var favoritesPage = new NavigationPage(new FavoritesPage())
{
Title = "Favorites",
Icon = "fave.png"
};
var aboutPage = new NavigationPage(new AboutPage())
{
Title = "About",
Icon = "info.png"
};
Children.Add(playPage);
Children.Add(favoritesPage);
Children.Add(settingsPage);
Children.Add(aboutPage);
}
}
I want to add a pause and play function to my app. On start up, the PlayPage would initially have the play.png icon and when I click on the PlayPage again it would change the icon to pause.png. Page is not changing just the page icon. Anyone has any idea how this could be done?
Edit:
So I have created a custom renderer, in OnElementChanged I utilize the ViewControllerSelected:
var tabbarController = (UITabBarController)this.ViewController;
if (null != tabbarController)
{
tabbarController.ViewControllerSelected += OnTabBarReselected;
}
And my OnTabBarReselected I have:
private void OnTabBarReselected(object sender, UITabBarSelectionEventArgs e)
{
switch (TabBar.SelectedItem.Title)
{
case "Play":
TabBar.SelectedItem.Title = "Pause";
TabBar.SelectedItem.Image = UIImage.FromFile("pause.png");
break;
}
}
This only does half of the work. It changes the Title of the selected tab bar from Play to Pause after I click on the same tab but not the Icon. The icon remains "play.png" until I get out of that tab page (selecting another tab). Anyone has any idea why?
You will need to implement a custom renderer to pull this off. There are some implementations on James Montemagno's blog where he talks about changing the icons.
iOS:
http://motzcod.es/post/138225183932/tintcolor-selectedimage-xamarin-forms-ios
Droid:
http://motzcod.es/post/157544468267/xamarin-forms-android-selected-and-unselected-tab-colors
This is however not necessarily related to your requirement of tapping the icon and changing that specific icon since all this code only runs when the page initially loads. It could be a nice starting point though. Check in there if there's a property on TabbedPage that changes when you tap on the current tab and change the icon at that point.
You also have a OnCurrentPageChanged event you can override in TabbedPage, but that isn't called when the page is already active.

Is there any way to pre-render a WebView control with Xamarin Forms?

I've created a set of custom html5 audio controls which I'm using in my iOS and Android Xamarin Forms app.
When I navigate to the page in my app where this content is located, the WebView renders as a white box for a fraction of a second before it renders the html content. Is there any way to render this view before it is visible to the user?
public partial class TutorialPage : ContentPage
{
private readonly ITutorialViewModel _dataModel;
public TutorialPage(ITutorialViewModel dataModel)
{
InitializeComponent();
Title = "Tutorial";
var browser = new WebView() { HeightRequest = 150, WidthRequest = 400, HorizontalOptions = LayoutOptions.CenterAndExpand };
browser.Source = DependencyService.Get<IBaseUrl>().Get();
mainStack.Children.Add(browser);
}
}
You can always use the events of the webview for that.
Take a look at them:
Navigated - Event that is raised after navigation completes.
Navigating - Event that is raised when navigation starts.
So what you have to do is set the webview's "IsVisible" property to false at the start, and when the Navigated event fires, set it back to true.
Example:
public partial class TutorialPage : ContentPage
{
private readonly ITutorialViewModel _dataModel;
private WebView browser;
public TutorialPage(ITutorialViewModel dataModel)
{
InitializeComponent();
Title = "Tutorial";
browser = new WebView() { HeightRequest = 150, WidthRequest = 400, HorizontalOptions = LayoutOptions.CenterAndExpand, IsVisible = false };
browser.Source = DependencyService.Get<IBaseUrl>().Get();
browser.Navigated += Browser_Navigated:
mainStack.Children.Add(browser);
}
void Browser_Navigated (object sender, WebNavigatedEventArgs e)
{
browser.IsVisible = true;
}
}
If you don't want to make the WebView as a global class variable, you can get it from the sender parameter of the Navigated event, as shown below:
void Browser_Navigated (object sender, WebNavigatedEventArgs e)
{
((WebView)sender).IsVisible = true;
}
I bypassed my problem by making the background transparent. The controls still pop in but it's better than having a white box on the page in the meantime.

Making a tabbed cef sharp browser rename tab page

i have been creating a cefsharp browser in C#. i have made it so you can have multiple tabs and it loads the pages correctly. however, i cannot seem to find how i can rename the tab to the name of the page.
this is the code in the load event for form1.cs:
Cef.Initialize();
toolTip1.SetToolTip(button1, "Settings");
TabPage tab = new TabPage();
Tab newtab = new Tab();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
tab.Controls.Add(newtab);
tabControl1.TabPages.Add(tab);
i have tried:
private void myBrowser_isLoading(object sender)
{
myBrowser.Parent.Parent.Text = myBrowser.Title;
}
but that doesn't work.
then this is the code for tab.cs:
public Tab()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
public CefSharp.WinForms.ChromiumWebBrowser myBrowser;
public bool nav = new bool();
public void InitializeChromium()
{
myBrowser = new CefSharp.WinForms.ChromiumWebBrowser("http://www.google.com");
this.Controls.Add(myBrowser);
myBrowser.Dock = DockStyle.Fill;
myBrowser.Parent = panel2;
if (nav == true)
{
myBrowser.Load(textBox1.Text);
nav = false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
again, i am using c# with the latest build of cef sharp(or atleast the one installed from the nuget package manager).
In your tab function in form1.cs, you need to add a title changed function like this
browser.TitleChanged += OnBrowserTitleChanged;
you also need to specify what browser is and set dockstyle to fill like this
ChromiumWebBrowser browser = new ChromiumWebBrowser("google.com");
tab.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
now for the OnBrowserTitleChanged, you will need an EventArg which will tell the tab to have the document title in this format
this.InvokeOnUiThreadIfRequired(() => browserTabControl.SelectedTab.Text = args.Title);
this will add the document title to the tabcontrol browserTabControl is the name of the tabcontrol you will have to change browserTabControl to whatever name you have for the tabcontrol. Also the code you have does not belong with cef initialize. you need to create an addNewTab method with all the functions that will be processed when you want to add a new tab. Also, you cannot use a panel if you want to have tabs. you need a tabcontrol

Binding a HTML control into a stacklayout using xamarin forms

Being a newbie to Xamrin I am struggling to adding some HTML to a StackLayout via Xamarin Forms. I have tried quite a few things and had a Google around.
Firstly I can't work out which bindable object I am supposed to be using. As I cannot find a straight answer on Google/Xamarin I am going to assume this is not as easy I was hoping.
var nameEntry = new Label ();
nameEntry.SetBinding (Label.TextProperty, "Club.ClubName");
var webView = new WebView ();
webView.SetBinding ( ??? , "Club.Description");
var content = new StackLayout {
Children = {
nameEntry,
???
}
};
I am not sure if this is possible within Xamarin forms itself. Can anyone help?
I should point out my data for the form is being retrieved asynchronously on a remote json endpoint
protected override void OnAppearing ()
{
base.OnAppearing ();
if (ViewModel == null || ViewModel.IsLoading)
return;
ViewModel.LoadItemsCommand.Execute (Club.ClubId);
}
My remote json api contains, Description contatins a HTML snippet which I would like to use.
{
ClubName: "Stourbridge",
Description: "<p>This club meets every TWO weeks on a <b>Friday</b>.</p>"
...
}
Try the following example that will show how to do the bindings.
Note that you have to use a HtmlWebViewSource to achieve this, and bind the WebView.Source to this.
Clicking the button will change the view model and update the WebView appropriately to the newly changed text.
StackLayout objStackLayout = new StackLayout();
MyView objMyView = new MyView();
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text</p></body></html>";
HtmlWebViewSource objHtmlWebViewSource = new HtmlWebViewSource();
objHtmlWebViewSource.SetBinding(HtmlWebViewSource.HtmlProperty, "MyHtml");
objHtmlWebViewSource.BindingContext = objMyView;
WebView objWebview = new WebView();
objWebview.HorizontalOptions = LayoutOptions.FillAndExpand;
objWebview.VerticalOptions = LayoutOptions.FillAndExpand;
objWebview.Source = objHtmlWebViewSource;
Button objMyButton2 = new Button();
objMyButton2.Text="Change Html";
objMyButton2.Clicked+=((o2,e2)=>
{
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text that has changed.</p></body></html>";
});
objStackLayout.Children.Add(objMyButton2);
objStackLayout.Children.Add(objWebview);
The view model is just a simple one with a bindable property as below:-
public class MyView
: Xamarin.Forms.View
{
public static readonly BindableProperty MyHtmlProperty = BindableProperty.Create<MyView, string>(p => p.MyHtml, default(string));
public string MyHtml
{
get { return (string)GetValue(MyHtmlProperty); }
set { SetValue(MyHtmlProperty, value); }
}
}
Before clicking the button gives:-
After clicking the button, adjusts the view model, and automatically updates the control via the binding giving:-

How do you display a custom UserControl as a dialog?

How do you display a custom UserControl as a dialog in C#/WPF (.NET 3.5)?
Place it in a Window and call Window.ShowDialog.
(Also, add references to: PresentationCore, WindowsBase and PresentationFramework if you have not already done so.)
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new MyUserControl()
};
window.ShowDialog();
}
Window window = new Window
{
Title = "My User Control Dialog",
Content = new OpenDialog(),
SizeToContent = SizeToContent.WidthAndHeight,
ResizeMode = ResizeMode.NoResize
};
window.ShowDialog();
Has worked like a magic for me.
Can it be made as a modal dialog?
Ans : ShowDialog it self make it as Modal Dialog.. ...
As far as I know you can't do that. If you want to show it in a dialog, that's perfectly fine, just create a new Window that only contains your UserControl, and call ShowDialog() after you create an instance of that Window.
EDIT:
The UserControl class doesn't contain a method ShowDialog, so what you're trying to do is in fact not possible.
This, however, is:
private void Button_Click(object sender, RoutedEventArgs e){
new ContainerWindow().ShowDialog();
}
namespace System.Window.Form
{
public static class Ext
{
public static DialogResult ShowDialog(this UserControl #this, string title)
{
Window wind = new Window() { Title = title, Content = #this };
return wind.ShowDialog();
}
}
}
The use of it maybe as simple as UserControlInstance.ShowDialog().
A better customized implementation would be by extending the Window class and customizing it using the the designer and code to get any functionality.
I know this is for .net 3.5, but here is a workable solution for .net 2.0
MyUserControl myUserControl= new MyUserControl();
Form window = new Form
{
Text = "My User Control",
TopLevel = true,
FormBorderStyle = FormBorderStyle.Fixed3D, //Disables user resizing
MaximizeBox = false,
MinimizeBox = false,
ClientSize = myUserControl.Size //size the form to fit the content
};
window.Controls.Add(myUserControl);
myUserControl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
window.ShowDialog();
You can also use MaterialDesignThemes.Wpf (downloadable on NuGet, .NET 4.5+). Then you can simply do:
{
var view = new YourUserControl();
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);
}
private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
{ } //Handle Closing here
If the answer by 'sixlettervariables' is modified as so, it works
private void button1_Click ( object sender, RoutedEventArgs e )
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UserControl ( ),
Height = 200, // just added to have a smaller control (Window)
Width = 240
};
window.ShowDialog ( );
}

Categories

Resources