How to set focus on richeditbox in metro style app? - windows-8

I have the following code:
private async void SendMsg_Click(object sender, RoutedEventArgs e)
{
RichEditBox.Document.SetText(TextSetOptions.None, "");
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if(RichEditBox!=null)
SendBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
});
}
but when clicked,the RichEditBox didnot got focus.What's wrong with my code? thanks
Sorry,I forgot add this code:"MsgWebView.NavigateToString("Hello World!");".And I found the problem lies in here.So the whole code is like this:
private void SendMsg_Click(object sender, RoutedEventArgs e)
{
MsgWebView.NavigateToString("Hello World!");
SendBox.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
How to solve this problem?
Best regards.

You need to use the Programmatic option on FocusState (not Keyboard).
SendBox.Focus(Windows.UI.Xaml.FocusState.Programmatic);

Related

UWP Loop Fade Animation

I have a Pivot and its header template is a user control and I want to start a continuous Flashing ( Fade Out Fade In ) animation on it on a specific trigger and then also want to stop it on a specific trigger, while both of these triggers/events will happen in the ViewModel of the page. I can take care of the triggers but my only issue is I am not sure how to achieve the infinite flashing behavior. The header has to start flashing when I tell it to and has to keep flashing until I tell it to stop. I wonder if there is some sort of Repeat animation property in xaml but couldn't find that in xaml animations from Community Toolkit
Queue is the model class which defines the context of HeaderTemplate, so I've custom events to start and stop the flashing, because I have access to that Queue object instance within the ViewModel of my page.
All the custom logic happens in code behind of the User Control. Attached is the code I have tried below. I would prefer if this is somehow done all in xaml or mostly in xaml so I can avoid any infinite loops or memory leak risks. My current attempt just freezes the UI and rightly so as I am doing an infinite loop which makes stuff just weird.
Code
Pivot Header
<Pivot.HeaderTemplate>
<DataTemplate x:DataType="cad:Queue">
<dataTemplates:HomePivotHeaderTemplate />
</DataTemplate>
</Pivot.HeaderTemplate>
Queue class
public partial class Queue
{
public event EventHandler<EventArgs> StartFlash;
public event EventHandler<EventArgs> StopFlash;
public void OnStartFlash() => StartFlash?.Invoke(this, EventArgs.Empty);
public void OnStopFlash() => StopFlash?.Invoke(this, EventArgs.Empty);
}
Triggers in ViewModel
public void StartFlashingPendingQueues()
{
PivotQueues.First()?.OnStartFlash();
}
private void StopFlashingPendingQueues()
{
PivotQueues.First()?.OnStopFlash();
}
UserControl for Data Template
public sealed partial class HomePivotHeaderTemplate : UserControl
{
public Queue VM => DataContext as Queue;
private bool _IsFlashing;
private bool _unLoaded;
public HomePivotHeaderTemplate()
{
InitializeComponent();
Loaded += HomePivotHeaderTemplate_Loaded;
Unloaded += HomePivotHeaderTemplate_Unloaded;
DataContextChanged += (s, e) => Bindings.Update();
}
private void HomePivotHeaderTemplate_Unloaded(object sender, RoutedEventArgs e)
{
_unLoaded = true;
Loaded -= HomePivotHeaderTemplate_Loaded;
Unloaded -= HomePivotHeaderTemplate_Unloaded;
VM.StartFlash -= VM_StartFlash;
VM.StopFlash -= VM_StopFlash;
}
private void HomePivotHeaderTemplate_Loaded(object sender, RoutedEventArgs e)
{
VM.StartFlash += VM_StartFlash;
VM.StopFlash += VM_StopFlash;
VM_Flashed();
}
private void VM_StopFlash(object sender, System.EventArgs e) => _IsFlashing = false;
private void VM_StartFlash(object sender, System.EventArgs e) => _IsFlashing = true;
private async Task VM_Flashed()
{
while (true)
{
if (_unLoaded)
{
break;
}
else
{
if (_IsFlashing)
{
await Block.Fade(value: 0f, duration: 500, delay: 0, easingType: EasingType.Linear, easingMode: EasingMode.EaseOut).Then().Fade(value: 1f, duration: 500, delay: 0, easingType: EasingType.Linear, easingMode: EasingMode.EaseIn).StartAsync();
}
}
}
}
}
Edit 1
If I can somehow figure out how to give infinite flashing animation to the textblock I can take care of the rest. basically I could just have 2 textblocks identical, one of them will be flashing other won't, and I can toggle their visibility based on triggers in my viewmodel.
I guess I was just overthinking it. It was a simple DoubleAnimation in a StoryBoard
UserControl code behind
public sealed partial class HomePivotHeaderTemplate : UserControl
{
public Queue VM => DataContext as Queue;
public HomePivotHeaderTemplate()
{
InitializeComponent();
Loaded += HomePivotHeaderTemplate_Loaded;
Unloaded += HomePivotHeaderTemplate_Unloaded;
DataContextChanged += (s, e) => Bindings.Update();
}
private void HomePivotHeaderTemplate_Unloaded(object sender, RoutedEventArgs e)
{
Loaded -= HomePivotHeaderTemplate_Loaded;
Unloaded -= HomePivotHeaderTemplate_Unloaded;
VM.StartFlash -= VM_StartFlash;
VM.StopFlash -= VM_StopFlash;
}
private void HomePivotHeaderTemplate_Loaded(object sender, RoutedEventArgs e)
{
VM.StartFlash += VM_StartFlash;
VM.StopFlash += VM_StopFlash;
}
private void VM_StopFlash(object sender, System.EventArgs e) => FlashStoryBoard.Stop();
private void VM_StartFlash(object sender, System.EventArgs e) => FlashStoryBoard.Begin();
}
UserControl Xaml
<UserControl.Resources>
<Storyboard x:Name="FlashStoryBoard">
<DoubleAnimation Storyboard.TargetName="Block" Storyboard.TargetProperty="Opacity"
Duration="0:0:2"
From="1"
RepeatBehavior="Forever"
To="0.2" />
</Storyboard>
</UserControl.Resources>
<TextBlock x:Name="Block"
Text="{x:Bind VM.Name}" />

Win RT Xaml GridView: Drag select multiple items

I'm trying to select multiple items in a GridView by hovering over them with pressed mouse (like drawing). I tried to achieve this with the PointerEntered event but I'm unable to change the selction from code. Is there a way to implement a custom selection mode?
This didn't work for me because I can't use Style.Triggers in Win RT XAML:
https://stackoverflow.com/a/2886223/5739170
You will have to inherit the gridview control and override the PrepareContainerForItemOverride method:
The code:
public class MyGridView : GridView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
(element as GridViewItem).PointerMoved += MyGridView_PointerMoved;
base.PrepareContainerForItemOverride(element, item);
}
private void MyGridView_PointerMoved(object sender, PointerRoutedEventArgs e)
{
//your logic for setting the isselected
(sender as GridViewItem).IsSelected = true;
}
}
This is how I finally implemented it based on Chirag Shah's answer:
class MyGridView : GridView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
(element as GridViewItem).PointerEntered += SelectItemOnEntered;
(element as GridViewItem).AddHandler(PointerPressedEvent, new PointerEventHandler(SelectItemOnPressed), true);
base.PrepareContainerForItemOverride(element, item);
}
private void SelectItemOnPressed(object sender, PointerRoutedEventArgs e)
{
(sender as GridViewItem).IsSelected = !(sender as GridViewItem).IsSelected;
}
private void SelectItemOnEntered(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact)
(sender as GridViewItem).IsSelected = !(sender as GridViewItem).IsSelected;
}
}
I hope this helps everyone who wants to implement this selection mode.

How to differentiate between the pageload and button click method in asp.net MVC 4 web application

I am new to MVC and don't know how to differentiate between a page load event and a button click event; for example this is the scenario according to asp.net webform application;
protected void Page_Load(object sender, EventArgs e)
{
//does something
}
and button click is as follows;
protected void btnLogin_Click(object sender, EventArgs e)
{
//does something
}
I have used so far in terms of MVC
[HttpPost]
public ActionResult Login()
{
//does something
return View();
}
[HttpPost]
public ActionResult Login(string username, string password)
{
//does something
return View();
}
I don't know except this. And it is obvious that IIS is giving ambiguity error as both have [HttpPost] attribute.
anyone know this, hope everyone except me, please advise me, thanks.

WP8 LongListSelector SelectedItem not bindable

In WP8, they forgot to provide SelectedItem as a dependency property, hence I'm not able to bind to it. I fixed that using this: http://dotnet-redzone.blogspot.com/2012/11/windows-phone-8longlistselector.html
On doing so, I'm noticing that I'm not able to reset the property from the ViewModel, i.e. if I set the item to null in the ViewModel, it does not impact the UI. I have already provided two way binding in the UI but still setting the item to null in the ViewModel does not change the selected item in the LongListSelector. I also don't want to use the SelectionChanged event as I'm sharing ViewModels between WP7.5 app and a WP8 app, hence I want to push as much as I can into the ViewModel. Is there a solution for this?
It appears that the custom LongListSelector class that you are using does not handle the setter properly.
Replace the OnSelectedItemChanged callback with the following:
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var selector = (LongListSelector)d;
selector.SetSelectedItem(e);
}
private void SetSelectedItem(DependencyPropertyChangedEventArgs e)
{
base.SelectedItem = e.NewValue;
}
And there is full version of these two parts:
public class LongListSelector : Microsoft.Phone.Controls.LongListSelector
{
public LongListSelector()
{
SelectionChanged += LongListSelector_SelectionChanged;
}
void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedItem = base.SelectedItem;
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register(
"SelectedItem",
typeof(object),
typeof(LongListSelector),
new PropertyMetadata(null, OnSelectedItemChanged)
);
private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var selector = (LongListSelector)d;
selector.SetSelectedItem(e);
}
private void SetSelectedItem(DependencyPropertyChangedEventArgs e)
{
base.SelectedItem = e.NewValue;
}
public new object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
}

Why Won't the Silverlight ChildWindow Display?

I have a simple Silverlight 4 application and have added a child window to it. I am using the below code to open it on a button click. This seems like it should work, does it not?
public void btnAbout_Click(object sender, RoutedEventArgs e)
{
About aboutThis = new About();
aboutThis.Show();
}
The "About" class looks like this:
public partial class About : ChildWindow
{
public About()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
I don't see any reason why it should not work.
Samples:
http://www.tanguay.info/web/index.php?pg=codeExamples&id=135
http://www.silverlighttoys.com/Tutorials.aspx?tutorial=2
What is your XAML like?
Try setting the Width and Height to 600px by 600px of your About Childwindow from xaml.