I am programming in C# for Windows Store apps but i am new to this platform. In my application i want to implement the share contract option when user click the Share button from Charm Bar.
I could see only SettingsPane and SearchPane API to access Settings and Search options but was not able to find anything with the name of SharePane. Is there any way to access the Share click handling in Windows Store apps ?
Here I am showing you the basic usage of share charm. In my example we will share text content from text box
DataTransferManager class does all the sharing. So first you will need the current instance of that class and invoke DataRequested event. So you have to assign event in OnNavigatedTo event & release event in OnNavigatedFrom event.
If you want to open share charm programmatically then write DataTransferManager.ShowShareUI(); in button's click event.
Code for the sample.
C#
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
dataTransferManager.DataRequested += ShareTextHandler;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
dataTransferManager.DataRequested -= ShareTextHandler;
}
private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequest request = e.Request;
request.Data.Properties.Title = "Share Text Example"; // You must have to set title.
request.Data.Properties.Description = "A demonstration that shows how to share text.";
request.Data.SetText(ShareText.Text);
}
private void Share_Click_1(object sender, RoutedEventArgs e)
{
DataTransferManager.ShowShareUI();
}
XAML
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBox Width="500" Height="200" x:Name="ShareText" />
<Button Content="Share" Click="Share_Click_1" HorizontalAlignment="Center" />
</StackPanel>
</Grid>
MSDN Sample App
Quickstart: Sharing content (Windows Store apps using C#/VB/C++ and XAML)
Related
I have an WPF User control which is is hosted in an Elementhost. I use elementhost to include an WPF user control in my classical Windows forms app.
Now, from Windows forms side I am trying to capture the mouseDown event that is produced in an WPF label but I don't know how to do it.
Any ideas?
A case might be able to help you. The winform form calls the wpf control.
Create a WPF custom control. The xaml code of the control is as follows.
<Grid>
<Image Margin="10,10,10,90" x:Name="img" Stretch="Uniform" Opacity="1">
<Image.BitmapEffect>
<DropShadowBitmapEffect Opacity="1" />
</Image.BitmapEffect>
</Image>
<TextBox Background="Transparent" Foreground="White" Height="40" FontSize="32" Margin="44,0,56,36" x:Name="txtBox1" Opacity="0.5" Text="" VerticalAlignment="Bottom" /> </Grid>
You need to add the corresponding function to set the effect. The code is as follows.
public void SetSource(string fileName)
{
img.Source = new BitmapImage(new Uri(fileName) );
}
public void SetOpacity(double opacity)
{
img.Opacity = opacity;
}
//
public string GetText()
{
return txtBox1.Text;
}
Create a Winform application and add a reference, otherwise the control will not work properly. The list of references is pictured below.
Regenerate the solution. On the left toolbar, a WPF control appears and drag it to the form.
Use the button control in the winform project to call the corresponding function.
private void button1_Click(object sender, EventArgs e)
{
((UserControl1)elementHost1.Child).SetSource(#"C:\Users\Admin\Pictures\Saved Pictures\9837f99502eba3d01d4fb671cab20c15.jpg");
}
private void button2_Click(object sender, EventArgs e)
{
((UserControl1)elementHost1.Child).SetOpacity(0.5);
}
private void button3_Click(object sender, EventArgs e)
{
string text = ((UserControl1)elementHost1.Child).GetText();
label1.Text = text;
}
Test items: The left side is the traditional Winform control. The right side is the imported WPF control. You can clearly see the "translucent" effect of the picture.
Not sure what exactly you're trying to achieve. Below is a simple example.You can edit the MouseDown event of the UserControl as needed.
If there is a problem, please make your problem clearer and show me the complete code sample that can reproduce your problem for analysis.
UserControl:
<Grid>
<Label x:Name="label" Content="Label" MouseDown="label_MouseDown" Background="AliceBlue" Width="300" Height="200" />
</Grid>
private void label_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("hello");
}
Add the UserControl reference in the WinForms project, drag and drop the UserControl on the Form1 designer after rebuilding the WinForms project.
The result of running the project and clicking the Label in the UserControl is shown in the figure.
I develop chat application on Xamarin.Forms.
I need to don't hide keyboard when I press Send button and hide keyboard when I tap anywhere else.
I made it for iOS.
Can I make the same for android?
Found a very simple method to achieve this goal, you can simply place a Button on the top of an Entry like this:
<Grid VerticalOptions="End">
<Entry x:Name="MessageEntry" TextChanged="MessageEntry_TextChanged_1" />
<Button x:Name="SendButton" Text="Send" HorizontalOptions="End" Clicked="Button_Clicked" IsEnabled="False" />
</Grid>
Code behind:
private void Button_Clicked(object sender, EventArgs e)
{
MessageEntry.Text = null;
}
private void MessageEntry_TextChanged_1(object sender, TextChangedEventArgs e)
{
if (MessageEntry.Text != null)
SendButton.IsEnabled = true;
else
SendButton.IsEnabled = false;
}
Tested on Android 6.0 emulator, works fine to me, you can customize the Button to make it looks more beautiful in this view:
you can have in your PCL a scrollview element in which you add you entry element, when you tap on the entry element it will bring the keyboard and not hide it, it only hide it when tapped on a different portion of the screen. let me know if it help
In Store Apps / Universal Apps / Windows Phone 8.1 Visual Studio 2013 projects, how to to select programmatically all the text in a TextBox with the contextual copy icon menu enabled like in the following screen shot :
(source: free.fr)
The need is to display a text in a context where there is a big probability that the user will want to copy it in the clipboard.
The following tests did not work :
XAML
<TextBox x:Name="MyTextBox" Grid.Row="0"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Text="The text I want to select"
IsReadOnly="True"
IsEnabled="True"
GotFocus="MyTextBox_GotFocus"></TextBox>
<Button x:Name="ButtonSelectAll" Grid.Row="1"
Content="Select All"
HorizontalAlignment="Center"
Click="ButtonSelectAll_Click"></Button>
C#
private void ButtonSelectAll_Click(object sender, RoutedEventArgs e)
{
MyTextBox.SelectAll();
// MyTextBox.Focus(FocusState.Programmatic);
}
private void MyTextBox_GotFocus(object sender, RoutedEventArgs e)
{
MyTextBox.SelectAll();
}
The try on Click event do nothing.
The try on GotFocus event select all the text but the contextual copy icon menu and the two handles are not shown.
If you add on the Click method "text.Focus(FocusState.Programmatic);" then the text is selected but the copy icon is not shown. And unfortunately, if you touch this text with you finger with the intention that the "copy icon" will appear, you lose the selection.
#efdummy Try this:
private void ButtonSelectAll_Click(object sender, RoutedEventArgs e)
{
MyTextBox.Select(0, MyTextBox.Text.Length);
string selectedText = MyTextBox.SelectedText;
DataPackage myPackage = new DataPackage();
myPackage.SetText(selectedText);
Clipboard.SetContent(myPackage);
}
I am developing a desktop application using Modern UI for WPF. I try to refresh my tab page when I go to a new tab page, but I couldn't.
I want to refresh my MUI WPF tab page when I go to another page using my tab controller.
Can anyone help me?
I'm not quite sure what you mean exactly, but by calling InvalidateVisual() on a control, you can force a visual refresh of it if that's what you're after, as it sounds like you've got a WPF control that isn't being updated when the data is changing.
Based on the MSDN documentation, this:
Invalidates the rendering of the element, and forces a complete new layout pass. OnRender is called after the layout cycle is completed.
For example:
var grid = new Grid();
// Our grid should refresh after this,
// although in normal circumstances it would by default regardless.
grid.InvalidateVisual();
I hope that this is of use.
You can use SelectionChanged event to handle this. You can refresh MUI WPF tab page by using SelectionChanged.
<TabControl x:Name="MyTab" SelectionChanged="MyTabControl_SelectionChanged">
<TabItem x:Name="TabItem1" Header="Tab 1"/>
<TabItem x:Name="TabItem2" Header="Tab 2"/>
<TabItem x:Name="TabItem3" Header="Tab 3"/>
</TabControl>
Then you can access to each TabItem at the event:
private void MyTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e){
if (TabItem1.IsSelected){}
if (TabItem2.IsSelected){}
if (TabItem3.IsSelected){}
}
While the selected answer is ok, it might not be the best way to go about it.
MUI includes a content navigation framework that handles content loading, unloading and history navigation based on link uris. If you want your content to be aware of navigation events such as loaded and unloaded events, you'll need to implement an interface.
Make your content navigation aware by implementing the IContent interface available in the FirstFloor.ModernUI.Windows namespace.
A simple Example is :
public class MyContent : UserControl, IContent
{
public void OnFragmentNavigation(FragmentNavigationEventArgs e)
{
}
public void OnNavigatedFrom(NavigationEventArgs e)
{
}
public void OnNavigatedTo(NavigationEventArgs e)
{
//Refresh your page here
}
public void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
// ask user if navigating away is ok
if (ModernDialog.ShowMessage("Navigate away?", "navigate", MessageBoxButton.YesNo) == MessageBoxResult.No) {
e.Cancel = true;
}
}
}
I'm trying to create a page in my app which will show simple text updates, and I'm planning on using a browser window to show this, so, to put it simply I want a browser windows within the app that will only show one page, I don't want the user to be able to navigate to another site.
I've got the browser window added in XAML like so:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:WebBrowser HorizontalAlignment="Left" Margin="12,6,0,0" Name="webBrowser1" VerticalAlignment="Top" Height="595" Width="438"/>
</Grid>
I've then got this in the C# file:
private void WebBrowser_OnLoaded(object sender, RoutedEventArgs e)
{
//SaveFilesToIsoStore();
webBrowser1.Navigate(new Uri("http://www.nokia.ie/"/*, UriKind.Relative*/));
}
Basically, first of all, am I doing this the right way, and if so, how do I get the browser to display the web page?
Thanks.
listen to the browser navigation and stop navigation to any sites that you haven't defined
private void WebBrowser_OnLoaded(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate(new Uri("http://www.nokia.ie/"));
webBrowser1.Navigating += OnNavigating;
}
private void OnNavigating(object sender, NavigatingEventArgs e)
{
//every navigation to any sites that is not www.nokia.ie will be blocked
if(e.Uri.OriginalString != "http://www.nokia.ie/")
{
e.Cancel=true;
}
}