Add remove partial view dynamically Prism - xamarin.forms

I meant to use partial views in the past but never did. Now I do have a need but for some reasons I cannot get it right. I build a noddy project
2 Partial Views
Add and remove each partial views.
Problem.
I seem to be able to Add/remove View first time ,second time round I cant.
I guess I am missing something but not sure what?
I cannot seem to be able to add "x:Name" in code.
Any suggstions?
thanks
Xaml
<StackLayout x:Name="MyStacklayout" HorizontalOptions="Center" VerticalOptions="Center">
<Button x:Name="AddViewOne" Clicked="AddViewOne_OnClicked" Text="Add View One"></Button>
<Button x:Name="AddViewTwo" Clicked="AddViewTwo_OnClicked" Text="Add View Two"></Button>
<Button x:Name="RemoveViewOne" Clicked="RemoveViewOne_OnClicked" Text="Remove View One"></Button>
<Button x:Name="RemoveViewTwo" Clicked="RemoveViewTwo_OnClicked" Text="Remove View Two"></Button>
<views:PartialViewOne x:Name="ViewOne" mvvm:ViewModelLocator.AutowirePartialView="{x:Reference self}" />
<views:PartialViewTwo x:Name="ViewTwo" mvvm:ViewModelLocator.AutowirePartialView="{x:Reference self}" />
</StackLayout>
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void AddViewOne_OnClicked(object sender, EventArgs e)
{
var myView = new PartialViewOne();
ViewModelLocator.SetAutowirePartialView(myView, this);
MyStacklayout.Children.Add(myView);
}
private void AddViewTwo_OnClicked(object sender, EventArgs e)
{
PartialViewTwo myView = new PartialViewTwo();
ViewModelLocator.SetAutowirePartialView(myView, this);
MyStacklayout.Children.Add(myView);
}
private void RemoveViewOne_OnClicked(object sender, EventArgs e)
{
var viewOneParent = (ViewOne.Parent as Layout<View>);
viewOneParent?.Children.Remove(ViewOne);
}
private void RemoveViewTwo_OnClicked(object sender, EventArgs e)
{
var viewOneParent = (ViewTwo.Parent as Layout<View>);
viewOneParent?.Children.Remove(ViewTwo);
}
}

Related

How to Simulate or Trigger Enter or Return key programmatically in Xamarin.Forms

I have a camera scanner app that scans barcodes and the result is returned to an Entry field.
I can focus that Entry field but I can't get it to press enter programmatically, is there a way to do this in Xamarin.Forms?
My Target platform is Android.
The Enter or Return key press would triger the Completed event. I use a button to set the focus to the entry and then simulate the Entry press in Focused event.
private void Button_Clicked(object sender, EventArgs e)
{
entry.Focus();
}
private void entry_Focused(object sender, FocusEventArgs e)
{
entry_Completed(sender, e);
}
private void entry_Completed(object sender, EventArgs e)
{
entry.Text = "Hello";
entry.Unfocus();
}
this is a small replication of the problem, which is fairly simple.
in XAML:
<Entry Name="entry" Completed="OnCompleted"/>
<Label Name="label"/>
<Button Text="PRESS ENTER" Clicked="OnButtonPressed"/>
in C# or code behind I have:
void OnCompleted(object sender, EventArgs e)
{
label.Text = entry.Text;
}
void OnButtonPressed (object sender, EventArgs e)
{
// we will be adding this code here at the last step
}
then create an Interface because the only easy way to do this is by using the native android functionality through a DependencyService:
public Interface IEnterService
{
void PressEnter();
}
Then in the Android Project you need to create a class for example: EnterService
and implement the previous created interface like this:
[assembly: Xamarin.Forms.Dependency(typeof(EnterService))]
namespace TestEnterPressed.Droid
{
[Register("sendKeyDownUpSync", "(I)V", "GetSendKeyDownUpSync_IHandler")]
class EnterService : IEnterService
{
public void PressEnter()
{
Console.WriteLine("ENTER IS PRESSED");
Instrumentation inst = new Instrumentation();
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
/* run your code here */
inst.SendKeyDownUpSync(Keycode.Enter);
}).Start();
}
}
}
Then in the last step use your DependencyService to simulate Enter pressed through a button other than the keyboard or after scanning a barcode with a camera you can do it like this:
void OnButtonPressed(object sender, EventArgs e)
{
entry.Focus();
DependencyService.Get<IEnterService>().PressEnter();
}
I hope this is useful to somebody, thanks a bunch everyone :)

How to disable CTRL keyboard in WPF DataGrid

I have a datagrid where in i want to prevent user from CTRL Key press.
<ImageBrush ImageSource="/AB17035_ZLT_Client;component/Images/bum_big.PNG"
Stretch="Uniform" Opacity="0.05" />
</Border.Background>
<DataGrid Name="PlanGrid" HorizontalAlignment="Stretch" FontSize="16" RowHeight="30" HorizontalScrollBarVisibility="Visible"
VerticalAlignment="Stretch"
AutoGenerateColumns="True"
SelectionMode="Extended"
VerticalGridLinesBrush="Transparent"
Background="Transparent" RowBackground="Transparent"
ItemsSource="{Binding PlanDataView, Mode=TwoWay}"
IsReadOnly="True" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" Cursor="Arrow" AllowDrop="True">
<DataGrid.InputBindings>
<KeyBinding Gesture="Ctrl" Command="ApplicationCommands.NotACommand"></KeyBinding>
</DataGrid.InputBindings>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<ei:CallMethodAction MethodName="PlanGrid_Loaded" TargetObject="{Binding}"></ei:CallMethodAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
If user tries for CTLR key press it should not work on wpf datagrid.
I googled and found the answer for it. Below is the piece of code to prevent from ctrl key press.
private void PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
e.Handled = true;
MessageBox.Show("CTRL Key is not allowed");
}
}
also add this function "PreviewKeyDown" to load event of data grid as below:
public void PlanGrid_Loaded(object sender, RoutedEventArgs e)
{
planDatagrid.KeyDown += new KeyEventHandler(PreviewKeyDown);
}
Code Behind
InitializeComponent();
dvSalesEntryDataGrid.PreviewKeyDown += Datagrid_PreviewKeyDown;
and Methods
private void Datagrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.RightCtrl||e.Key==Key.LeftCtrl)
{
e.Handled = true;
}
}

Asp.net form links

How can I use a link in asp.net as a button. What's the right way to do this? For example with a label I'm doing it like this:
<a class="navbar-brand" href="#">Welkom<asp:Label ID="txtWelkom" runat="server" Text=""></asp:Label></a>
And then I set the label to a value that I received from the server for example:
txt.Welkom = name;
So how can I do this with a link (without seeing the button).
Is this the right way to do it (in asp.net forms)?
You want the LinkButton Control:
On .aspx side
<asp:LinkButton ID="lnkWelkom" runat="server" OnClick="lnkWelkom_OnClick" />
Code behind (aspx.cs)
using System;
using System.Web.Security.AntiXss;
using System.Web.UI;
namespace StackOverflowExamples.WebForms
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var name = "";
lnkWelkom.Text = AntiXssEncoder.HtmlEncode(name, true);
}
protected void lnkWelkom_OnClick(object sender, EventArgs e)
{
//Do stuff
}
}
}

Submit is not working

I'm using dynamic controls at my ASP.NET applications. So the issue is - when I use these controls separatelly - they work well. But when I put these two controls on the same page - ONLY LAST submit event is fired. Could anyone help me to identify where is the issue and how to fix it?
Here is my aspx layout:
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="MYPAGE.ascx.cs" Inherits="MYNAMESPACE" %>
<%# Register Assembly="MYANOTHERNAMESPACE" Namespace="MYNAMESPACE" TagPrefix="DAControl" %>
...
<div>
<DAControl:ChooseImageDialog runat="server" id="ChooseImageDialog" />
<DAControl:ChooseVideoDialog runat="server" id="ChooseVideoDialog" />
</div>
Here is my 1st control:
[ToolboxData("<{0}:ChooseImageDialog runat=server></{0}:ChooseImageDialog>")]
public class ChooseImageDialog : WebControl
{
/* This is only part of my code, which should enough for explaining the issue*/
private Button applyPreview = new Button();
private Button cancelPreview = new Button();
protected override void OnLoad(EventArgs e)
{
applyPreview.Click += new EventHandler(applyPreview_Click);
base.OnLoad(e);
}
void applyPreview_Click(object sender, EventArgs e)
{
// I want to reach this block
}
}
}
Here is my 2nd control:
[ToolboxData("<{0}:ChooseVideoDialog runat=server></{0}:ChooseVideoDialog>")]
public class ChooseVideoDialog : WebControl
{
/* This is only part of my code, which should enough for explaining the issue*/
private Button applyVideoPreview = new Button();
private Button cancelPreview = new Button();
protected override void OnLoad(EventArgs e)
{
applyVideoPreview.Click += new EventHandler(ApplyPreviewVideo_Click);
base.OnLoad(e);
}
void ApplyPreviewVideo_Click(object sender, EventArgs e)
{
// I want to reach this block
}
}
Events must be wired up in the Init event. Any later in the life cycle is too late.

Handling User Control Events on Containing Page

I want to have a user control containing a save button.
I want the click event attached to the save button to be handled by the containing page not the user control
Is this possible?
EDIT MORE SPECIFIC
Say I have a page called "Editor Page" and a user control called "Editor Buttons".
On the "Editor Buttons" user control I have a save button (which is a web control). I want the Click event of the save button to be handled by the "Editor Page" rather than the user control.
The reason for this is because I want to specify base pages that implement common behaviour.
This technique is commonly referred to as “event bubbling”.
Here is an exmple:
public partial class Test : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Button1_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
Then you can subscribe the event buttonClick at webpage to display the different information .
public partial class Test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControlID.buttonClick+=new EventHandler(UserControlID_buttonClick);
}
protected void UserControlID_buttonClick(object sender, EventArgs e)
{
Response.Write("hello,I am jack.");
}
}
I will assume that you have the code for the UserControl. If that is the case, you can define a Save event in your user control and have it expose the Click event of the save button, either directly, or by internally setting up an event handler and raise the Save event when the button is clicked. That could look something like this in the UserControl:
public event EventHandler Save;
private void SaveButton_Click(object sender, EventArgs e)
{
OnSave(e);
}
protected void OnSave(EventArgs e)
{
// raise the Save event
EventHandler save = Save;
if (save != null)
{
save(this, e);
}
}
UserControl1.ascx
UserControl1.ascx.cs
public partial class UserControl1 : System.Web.UI.UserControl
{
public event EventHandler UserControl1Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, EventArgs e)
{
UserControl1Click(sender, e);
}
}
UserControl2.ascx
UserControl1.ascx.cs
public partial class UserControl2 : System.Web.UI.UserControl
{
public event EventHandler UserControl2Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn2_Click(object sender, EventArgs e)
{
UserControl2Click(sender, e);
}
}
Then add one Asp.Net Text box(to display user control id) and above two user controls to Aspx page as shown below.
<title>Untitled Page</title>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox> <br />
<UC1:UC ID="uc1" runat="server" />
<UC1:UC ID="uc2" runat="server" />
</div>
</form>
Now add event handler for each user control to handle button click event of two controls as shown below.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
uc1.UserControl1Click += new EventHandler(uc1_UserControl1Click);
uc2.UserControl2Click += new EventHandler(uc2_UserControl2Click);
}
protected void uc1_UserControl1Click(object sender, EventArgs e)
{
txt1.Text = "User Control 1 Clicked";
}
protected void uc2_UserControl2Click(object sender, EventArgs e)
{
txt1.Text = "User Control 2 Clicked";
}
}
If we click User Control1, text box displays “User Control 1 Clicked” or if we click on User Control2, text box displays “User Control 2 Clicked”.

Resources