How to add progress bar or progress ring for webview project for windows mobile app - windows-phone-8

I have created a Webview Project for windows phone app to load google.com. It's working fine but I'm unable to add a Progress bar or Progress Ring. Can any one please help me?
namespace App2
{
public sealed partial class MainPage : Page
{
private static readonly Uri HomeUri = new Uri("http://www.google.com", UriKind.Absolute);
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
WebViewControl.Navigate(HomeUri);
HardwareButtons.BackPressed += this.MainPage_BackPressed;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
HardwareButtons.BackPressed -= this.MainPage_BackPressed;
}
private void MainPage_BackPressed(object sender, BackPressedEventArgs e)
{
if (WebViewControl.CanGoBack)
{
WebViewControl.GoBack();
e.Handled = true;
}
}
private void Browser_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
if (!args.IsSuccess)
{
Debug.WriteLine("Navigation to this page failed, check your internet connection.");
}
}
}
}

In your XAML add a progress ring over the webview so that it overlaps webview for example
<Grid>
<Grid x:Name="webViewHolder" >
<WebView x:Name="wvPage" Loaded="WebView_Loaded" NavigationCompleted="WebView_NavigationCompleted" NavigationStarting="wvPage_NavigationStarting"></WebView>
</Grid>
<ProgressRing x:Name="myProgressRing" IsActive="True" Height="90" Width="90" Background="Transparent" Foreground="#EF4D17"/>
</Grid>
Now in code Behind
private void wvPage_NavigationStarting(Windows.UI.Xaml.Controls.WebView sender, WebViewNavigationStartingEventArgs args)
{
myProgressRing.IsActive = true;
}
.
.
.
private void WebView_NavigationCompleted(Windows.UI.Xaml.Controls.WebView sender, WebViewNavigationCompletedEventArgs args)
{
myProgressRing.IsActive = false;
}

Related

Frame.GoBack minimizes the Application

public Constructor1()
{
this.NavigationCacheMode = NavigationCacheMode.Enabled;
}
....
public Constructor2()
{
this.NavigationCacheMode = NavigationCacheMode.Enabled;
HardwareButtons.BackPressed += this.MainPage_BackPressed;
}
private void MainPage_BackPressed(object sender, BackPressedEventArgs e)
{
if(this.Frame.CanGoBack)
{
this.Frame.GoBack();
}
}
After I use the BackButton on my Windows Phone my Application minimizes itself. I have to go to the Task Manager and click on it to bring it back up.
Any suggestions why Frame.GoBack minimizes the Application?
private void MainPage_BackPressed(object sender, BackPressedEventArgs e)
{
if(this.Frame.CanGoBack)
{
e.Handled = true;
this.Frame.GoBack();
}
}
e.Handled = true; was the problem.

How to handle Tap event from Hold Event in WindowsPhone8?

I have a usercontrol and tap event which is in usercontrol itself..And am having holding event for that usercontrol in phoneapplication page which is parent page.I want to raise tap event from hold event how do i achieve this?..
ParentPage is having:
<DataTemplate x:Key="template" >
<chatbubble:ChatBubbleControl x:Name="ChatBubble" Hold="ChatBubbleControl_Hold_1" />
</DataTemplate>
UserControl is having..
<UserControl.Resources>
....
<Grid x:Name="LayoutRoot" Background="Transparent" Width="455" Tap="chatBubble_Tap" >
.....
</Grid>
I want to raise chatBubble_Tap event from ChatBubbleControl_Hold_1
You can try to raise your event like this:
// make your eventhandler in Parent Page public and static so it will be available thru all the App
public static void chatBubble_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
// your code
}
// then in your UserControl you should be able to call it like this:
private void ChatBubbleControl_Hold_1(object sender, System.Windows.Input.GestureEventArgs e)
{
YourPage.chatBubble_Tap(sender, e);
}
This case depends on what you have in your Tap event (not everything can be in static method).
You can also pass the handler of your Page to your UserControl and then invoke your Tap event from handler (in this case the Tap event can be public (non-static). Simple case can look like this:
// your Control in MainPage (or other Page)
<local:myControl x:Name="yourControl" VerticalAlignment="Center" Grid.Row="1"/>
// initializing control and event to be invoked:
public MainPage()
{
InitializeComponent();
yourControl.pageHandler = this;
}
public void second_Click(object sender, RoutedEventArgs e)
{
// something here
}
// and the control code:
public partial class myControl : UserControl
{
public Page pageHandler;
public myControl()
{
InitializeComponent();
myButton.Hold +=myButton_Hold;
}
private void myButton_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
if (pageHandler is MainPage) (pageHandler as MainPage).second_Click(sender, e);
}
}
The third option would be to pass an Action to your Control (in this case event can be private):
// code in MainPage (or your Page)
public MainPage()
{
InitializeComponent();
yourControl.myAction = second_Click; // setting an action of Control
}
private void second_Click(object sender, RoutedEventArgs e)
{
// something here
}
// and the Control class
public partial class myControl : UserControl
{
public Action<object, System.Windows.Input.GestureEventArgs> myAction;
public myControl()
{
InitializeComponent();
myButton.Hold +=myButton_Hold;
}
private void myButton_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
if (myAction != null) myAction.Invoke(sender, e);
}
}

ScrollViewer not scroll up while Keyboard is active

How I can get behavior of form in windows Phone like Contacts >> New contacts >> Name. In this page it have many textboxes in scrollviewer. When user taps on any textbox and its get focus then the page scrolls up and header remains constant and SIP keyboard shown.
This is a my example but not it works
https://app.box.com/s/lxxcmxp8ckuottrweg52
Why?
thank you
I have modified the above code that works fine for as below.
public double OldHeight;
private TranslateTransform _translateTransform;
#region TranslateY dependency property
public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register(
"TranslateYProperty", typeof(double), typeof(Chat), new PropertyMetadata(default(double), PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var chat = o as Chat;
#if DEBUG
Debug.WriteLine("New value:" + e.NewValue);
Debug.WriteLine("Old value:" + e.OldValue);
#endif
if (chat != null)
{
chat.UpdateTopMargin((double)e.NewValue);
}
}
public double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
set { SetValue(TranslateYProperty, value); }
}
#endregion
private void ChatPage_OnLoaded(object sender, RoutedEventArgs e)
{
var transform = ((Application.Current).RootVisual).RenderTransform as TransformGroup;
if (transform != null)
{
_translateTransform = transform.Children.OfType<TranslateTransform>().FirstOrDefault();
if (_translateTransform != null)
{
var binding = new Binding("Y")
{
Source = _translateTransform
};
BindingOperations.SetBinding(this, TranslateYProperty, binding);
}
}
}
private void UpdateTopMargin(double translateY)
{
LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0);
}
Thanks
First, name your scroll viewer ScrollViewer. After that add the GotFocus and LostFocus event handlers for each text box control on the page and write the following code inside:
private void txt_LostFocus(object sender, RoutedEventArgs routedEventArgs)
{
ScrollViewer.Height = _oldHeight;
}
void txt_GotFocus(object sender, RoutedEventArgs e)
{
var transform = ((Application.Current).RootVisual).RenderTransform as TransformGroup;
if (transform != null)
{
_translateTransform = transform.Children.OfType<TranslateTransform>().FirstOrDefault();
if (_translateTransform != null)
{
var binding = new Binding("Y")
{
Source = _translateTransform
};
BindingOperations.SetBinding(this, TranslateYProperty, binding);
}
}
var clipboardVisible = false;
try
{
clipboardVisible = Clipboard.ContainsText();
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
}
ScrollViewer.Height = _oldHeight - (clipboardVisible ? 407 : 338);
}
You will need to add the following dependency property to the page:
#region TranslateY dependency property
public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register(
"TranslateYProperty", typeof(double), typeof(OrderContactPage), new PropertyMetadata(default(double), PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((OrderContactPage)o)._translateTransform.Y = 0;
}
public double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
set { SetValue(TranslateYProperty, value); }
}
#endregion
And also helper fields:
private double _oldHeight;
private TranslateTransform _translateTransform;
You also need to handle some events for your scroll viewer, add this to the page's constructor:
ScrollViewer.Loaded += ScrollViewerOnLoaded;
ScrollViewer.SizeChanged += ScrollViewer_OnSizeChanged;
Implement those event handlers:
private void ScrollViewerOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
ScrollViewer.Loaded -= ScrollViewerOnLoaded;
_oldHeight = ScrollViewer.ActualHeight;
}
private async void ScrollViewer_OnSizeChanged(object sender, SizeChangedEventArgs e)
{
await ScrollToFocusedElement();
}
private async Task ScrollToFocusedElement()
{
await Task.Yield();
var focusedElement = FocusManager.GetFocusedElement() as PhoneTextBox;
if (focusedElement != null)
{
// http://stackoverflow.com/questions/1225318/how-can-i-make-the-silverlight-scrollviewer-scroll-to-show-a-child-control-with
var focusedVisualTransform = focusedElement.TransformToVisual(ScrollViewer);
var rectangle =
focusedVisualTransform.TransformBounds(
new Rect(new Point(focusedElement.Margin.Left, focusedElement.Margin.Top), focusedElement.RenderSize));
var offset = ScrollViewer.VerticalOffset + (rectangle.Bottom - ScrollViewer.ViewportHeight);
ScrollViewer.ScrollToVerticalOffset(offset);
}
}
Wow, there is a lot of code there. I am working on creating something reusable, but I am not there yet. Once I do it, I will publish it on NuGet.
Note that this only works in Portrait mode and will not work perfectly if you have the auto suggestion bar open. I didn't need to handle that in my app so I skipped it :)
Enjoy.
you can try this sample sample project
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0"
Background="#002080">
<TextBlock Text="PAGE HEADER" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<Grid Grid.Row="1" />
<TextBox Grid.Row="2"
Text=""
x:Name="messageBox"
Background="White" LostFocus="MessageBox_OnLostFocus" />
</Grid>
public MainPage()
{
InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private static double _newValue;
private static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register("TranslateY", typeof(double), typeof(MainPage), new PropertyMetadata(0d, OnRenderXPropertyChanged));
private double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
}
private static void OnRenderXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((double)e.NewValue <= _newValue)
((MainPage)d).UpdateTopMargin((double)e.NewValue);
_newValue = (double)e.NewValue;
}
private void BindToKeyboardFocus()
{
var frame = Application.Current.RootVisual as PhoneApplicationFrame;
if (frame == null) return;
var group = frame.RenderTransform as TransformGroup;
if (#group == null) return;
var translate = #group.Children[0] as TranslateTransform;
var translateYBinding = new Binding("Y") { Source = translate };
SetBinding(TranslateYProperty, translateYBinding);
}
private void UpdateTopMargin(double translateY)
{
double prevTopMargin = LayoutRoot.Margin.Top;
LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0);
}

caliburn winrt usercontrol and capturing custom event

I made my own slider as user control with some custom properties and one custom event. Everything works fine, but recently I start using Caliburn Micro, and I don't know how to capture my custom event.
Previously I used:
<my:RadialSlider x:Name="slider" WedgeAngle="270" ..... AngleChanged="slider_AngleChanged" />
and
public void slider_AngleChanged(object sender, ValueChangedEventArgs e)
{
.... something ....
}
Now, in Caliburn project I tried:
<my:RadialSlider x:Name="slider" WedgeAngle="270" ..... cal:Message.Attach="[Event AngleChanged] = [Action slider_AngleChanged($eventArgs)]" />
and in my ViewModel:
public void slider_AngleChanged(object sender, ValueChangedEventArgs e)
{
.... something ....
}
But, this doesn't work...
So, how to capture this event?
Slider UC code-behind:
public delegate void AngleChangedEventHandler(object sender, ValueChangedEventArgs e);
public sealed partial class RadialSlider : UserControl
{
public event AngleChangedEventHandler AngleChanged;
private void OnAngleChanged(ValueChangedEventArgs e)
{
if (AngleChanged != null)
AngleChanged(this, e);
}
public static readonly DependencyProperty WedgeAngleProperty = DependencyProperty.Register("WedgeAngle", typeof(double), typeof(RadialSlider), new PropertyMetadata((double)270, new PropertyChangedCallback(OnPropertyWedgeAngleChanged)));
public double WedgeAngle
{
get { return (double)this.GetValue(WedgeAngleProperty); }
set { this.SetValue(WedgeAngleProperty, value); }
}
private static void OnPropertyWedgeAngleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as RadialSlider).UpdateControls();
if (e.NewValue != e.OldValue)
{
(sender as RadialSlider).OnAngleChanged(new ValueChangedEventArgs((double)e.OldValue, (double)e.NewValue));
}
}
}
You need to use a routed event. This has to do with how events bubble up the visual tree and how Caliburn.Micro attaches itself to them. Standard events should be avoided on controls or UI widgets in any tech using Xaml as the loose out on some pretty funky features (bubble up / down).

JPopupMenu doesn't dissapear/move when minimising/moving the JFrame it is in

I am making an mp3 player, with several JLists in my JFrame. When I right click on a JList item, a popup with some options for that song appears. But when this popup is visible, and I minimise my JFrame, this popup stays visible! Also, when the popup is visible, and I drag my JFrame to somewhere else on the screen, the popup stays on it's original position (so it does not stay on the same position relative to the JFrame)... Can someone please help me out with this? I tried to strip down this class as much as possible :)
I would be really grateful if someone could help me out !!
Joe
public class playListPanel extends JPanel implements MouseListener {
private DefaultListModel model;
private Interface interFace;
private JList list;
private boolean emptyPlaylist;
private ArrayList<Song> currentPlayList;
private Song rightClickedSong;
private JPopupMenu popup;
private Point panelLocation;
public playListPanel(Interface interFace) // Interface extends JFrame,
// playListPanel is a part of
// this JFrame.
{
this.interFace = interFace;
this.panelLocation = new Point(559, 146);
setBackground(SystemColor.controlHighlight);
setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
TitledBorder.TOP, null, null));
setBounds((int) panelLocation.getX(), (int) panelLocation.getY(), 698,
368);
setLayout(null);
currentPlayList = new ArrayList<Song>();
model = new DefaultListModel();
list = new JList(model);
list.setVisible(true);
list.addMouseListener(this);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setBounds(5, 5, 688, 357);
add(scrollPane);
emptyPlaylist = true;
}
private void openMenuPopup(Point point)
{
removePopup();
popup = new JPopupMenu();
int x = (int) point.getX();
int y = (int) point.getY();
popup.setLocation((int) (x+panelLocation.getX()),(int) (y+panelLocation.getY()));
//popup.setLabel("popup voor playlist");
JMenuItem removeSong;
popup.add(removeSong = new JMenuItem("Remove Song from Playlist", new ImageIcon("image.jpg")));
ActionListener menuListener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("Remove Song from Playlist"))
{
System.out.println("Remove Song from Playlist");
interFace.getPlaylistManager().removeOneSong(rightClickedSong);
removePopup();
}
};
//ADD THE LISTENERS TO THE MENU ITEMS
removeSong.addActionListener(menuListener);
popup.setVisible(true);
}
public void removePopup()
{
if(popup!==null)
{
popup.setVisible(false);
System.out.println("popup removed");
}
}
private int getRow(Point point) {
return list.locationToIndex(point);
}
public void refreshPlayList(ArrayList<Song> playlist) {
this.currentPlayList = playlist;
model.clear();
for (Song song : playlist) {
model.add(model.getSize(), song.getPlaylistString());
}
list.setVisible(true);
}
public void highlightSong(int index) {
list.setSelectedIndex(index);
}
public int getRowOfList(Point point) {
return list.locationToIndex(point);
}
#Override
public void mouseClicked(MouseEvent e) {
interFace.getPlaylistManager().doubleClickOnPlaylist(e);
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
rightClickedSong = currentPlayList.get(getRow(e.getPoint()));
openMenuPopup(e.getPoint());
System.out.println("should open popup at "
+ e.getPoint().toString());
}
}
#Override
public void mouseReleased(MouseEvent arg0) {
}
}
There are some basic flaws in the way you are handling click for showing popup.
It is not advisable to call popup.setVisible in simple scenarios like this. Instead, you may rely on its default behavior. Also, better to use e.isPopupTrigger() than to check SwingUtilities.isRightMouseButton(e) to show popup.
You may do something like the following :
//at classlevel,
private JPopupMenu popup = new JPopupMenu();
//create a Popuplistener
PopupListener pl = new PopupListener();
list.addMouseListener(pl);
//Implementation of your popuplistener
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger())
//e.getSource - and construct your popup as required.
//and then.
popup.show(((JApplet) e.getComponent()).getContentPane(), e
.getX(), e.getY());
}
}