I'm writing my first metro-style app. Days ago I've written the code for taking photos based on this sample (here) and it works. With the release of Windows 8 release preview and visual studio 2012 release candidate, the same snippet doesn't work. It seems that there is a problem with the access to camera but in Package.appxmanifest I've checked the webcam capability.
the xaml :
<Canvas x:Name="previewCanvas1" Width="320" Height="240" Background='Gray'>
<Image x:Name="imageElement1" Width="320" Height="240" Visibility="Collapsed"/>
<CaptureElement x:Name="previewElement1" Width="320" Height="240" />
</Canvas>
<StackPanel Orientation="Horizontal" Margin="20" HorizontalAlignment="Center">
<Button Width="120" x:Name="btnStartPreview2" Click="btnStartPreview_Click" IsEnabled="true" Margin="0,0,10,0" Background="#FFC3C3C3">Da Webcam</Button>
<Button Width="120" x:Name="btnTakePhoto2" Click="btnTakePhoto_Click" IsEnabled="false" Margin="0,0,10,0" Background="#FFC3C3C3">Scatta</Button>
</StackPanel>
And the code behind:
MediaCapture mediaCaptureMgr;
async void btnStartPreview_Click(Object sender, RoutedEventArgs e)
{
try
{
mediaCaptureMgr = new Windows.Media.Capture.MediaCapture();
await mediaCaptureMgr.InitializeAsync();
previewElement1.Source = mediaCaptureMgr;
await mediaCaptureMgr.StartPreviewAsync();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
the exception is: "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" but as said, the webcam capability is checked!
I solved it. To use the webcam you must indicate in package.appxmanifest that the application needs to access the webcam and microphone. Strange but true!
Related
I'm trying to implement color picker in uwp using below link
[1]: http://www.c-sharpcorner.com/article/coding4fun-colorpicker-control-in-uwp-with-xaml-and-c-sharp/
but while following process and implementing colorchange event it is giving error "unable to add event handler".Any idea would be appreciated
XAML
xmlns:my="using:Coding4Fun.Toolkit.Controls"
<my:ColorPicker x:Name="W_Paints"
Margin="216,203,-6,0" Height="40"
Width="40" VerticalAlignment="Top"
HorizontalAlignment="Left"/>
I tried to create a color picker with Coding4Fun package by following the above link and the color picker is successfully created with no error on my side.
I used version 2.1.8, and also test version 2.1.7 which also worked. My uwp app target version is build 14393, but I also test with target version 10240. So if you created a uwp app with "Coding4Fun Toolkit - Controls" 2.1.7 or 2.1.8 should be able work well. Here is the demo completing code.
XAML Code
<Page
x:Class="Coding4fun.MainPage"
...
xmlns:my="using:Coding4Fun.Toolkit.Controls" >
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="419,42,0,0" TextWrapping="Wrap" Text="Code4Fun ColorPicker control Demo" VerticalAlignment="Top" Height="37" Width="427" FontSize="24" FontWeight="Bold" />
<Button x:Name="btnCPopen" Content="Open Color Picker" HorizontalAlignment="Left" Margin="110,113,0,0" VerticalAlignment="Top" RenderTransformOrigin="-5.01,1.529" ToolTipService.ToolTip="Open color Picker for changing Background" Click="btnCPopen_Click" />
<Border x:Name="BorCP" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="378" Margin="838,113,0,0" VerticalAlignment="Top" Width="354" />
<my:ColorPicker x:Name="CPtest" HorizontalAlignment="Left" Height="358" Margin="284,113,0,0" VerticalAlignment="Top" Width="374" ColorChanged="CPtest_ColorChanged" Visibility="Collapsed" />
</Grid>
Code behind
private void btnCPopen_Click(object sender, RoutedEventArgs e)
{
CPtest.Visibility = Visibility;
}
private void CPtest_ColorChanged(object sender, Windows.UI.Color color)
{
BorCP.Background = new SolidColorBrush(color);
}
I also upload the demo here you can download for testing and compare what's wrong with your project.
I have the following XAML code for Windows Phone 8.1 (non SilverLight):
<Grid>
<ToggleButton Name="TogBtn" VerticalAlignment="Center" HorizontalAlignment="Center" Checked="ToggleButton_OnChecked">
<SymbolIcon Symbol="play"></SymbolIcon>
</ToggleButton>
</Grid>
The output of the above code is:
How can I change the icon to a stop icon when the toggle button is checked and then back to play icon when unchecked?
I thought this would be easy to find through Google, but apparently not.
Please change your XAML to this:
<Grid>
<ToggleButton x:Name="TogBtn" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked">
<SymbolIcon Symbol="Play"></SymbolIcon>
</ToggleButton>
</Grid>
And please add this to your .cs file:
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
TogBtn.Content = new SymbolIcon(Symbol.Stop);
}
private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
TogBtn.Content = new SymbolIcon(Symbol.Play);
}
That should do the job!
I have an image control in my main page and the code is as follows:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel HorizontalAlignment="Left" Height="597" VerticalAlignment="Top" Width="440">
<Image x:Name="hinh1" Height="488" Stretch="Fill"/>
<ProgressBar Name="loading" Height="10" IsIndeterminate="True" Visibility="Collapsed"/>
</StackPanel>
</Grid>
and in code behind i have this code :
Uri hinh = new Uri
("http://taigamejar.net/wp-content/uploads/2014/01/Hinh-Anh-Dep-5.jpg", UriKind.Absolute);
hinh1.Source = new BitmapImage(hinh);
While waiting for the image to load, I want to call progress bar run to inform the user that it is loading. Once the the image has loaded, the progress bar should disappear. How can I do this?
If I were you, I would prefer to use , not ProgressBar.
So, I'll give
protected override void OnNavigatedTo(NavigationEventArgs e)
{
loading.IsActive = true;
Uri hinh = new Uri
("http://taigamejar.net/wp-content/uploads/2014/01/Hinh-Anh-Dep-5.jpg", UriKind.Absolute);
hinh1.Source = new BitmapImage(hinh);
hinh1.ImageOpened+=hinh1_ImageOpened; //loadingbar will be disappear when this triggered
}
private void hinh1_ImageOpened(object sender, RoutedEventArgs e)
{
loading.IsActive = false; //this will disable the progressring.
}
And XAML:
<StackPanel HorizontalAlignment="Left" Height="597" VerticalAlignment="Top" Width="400">
<Image x:Name="hinh1" Height="488" Stretch="Fill" ImageOpened="hinh1_ImageOpened"/>
<ProgressRing Name="loading" Height="109" IsActive="True" />
</StackPanel>
If you don't have WP8.1 SDK yet, you can get ProgressRing here: http://www.onurtirpan.com/onur-tirpan/english/windows-phone-english/using-progressring-in-windows-phone/
is it possible to get a watermarks passwordbox in WinRt? It is no problem to get a textbox with a watermark, but I don't know a toolkit where I can get a password box with a watermark.
How can I implement one for myself?
Take a look on WinRT XAML Toolkit.
They also have
WatermarkTextBox
WatermarkPasswordBox
By yourself you can implement your own controls:
in .xaml:
<Border x:Name="brdPassword" Margin="5,0,5,10" BorderThickness="2" BorderBrush="White" CornerRadius="5" Grid.Row="0"
Background="White" Height="50" VerticalAlignment="Stretch">
<Grid>
<TextBox x:Name="PasswordWatermark" TextWrapping="Wrap"
Text="Watermark" Foreground="#FFC4C4C4" IsHitTestVisible="False"
Background="{x:Null}" BorderThickness="0" Padding="0,-10"
FontSize="26.667" />
<PasswordBox x:Name="pbPassword" LostFocus="PasswordLostFocus"
GotFocus="PasswordGotFocus" Background="{x:Null}"
FontSize="26.667" Margin="0,-12,0,-9" VerticalAlignment="Center"
BorderThickness="0" Opacity="0" />
</Grid>
</Border>
in .cs
private void PasswordLostFocus(object sender, RoutedEventArgs e)
{
CheckPasswordWatermark();
}
private void CheckPasswordWatermark()
{
var passwordEmpty = string.IsNullOrEmpty(pbPassword.Password);
PasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
pbPassword.Opacity = passwordEmpty ? 0 : 100;
}
private void PasswordGotFocus(object sender, RoutedEventArgs e)
{
PasswordWatermark.Opacity = 0;
pbPassword.Opacity = 100;
}
Hope it's help
I don't think we can put watermark in the Password control.
You can put a TextBox with wartermark in the same row and same column with the Password control, then handle the two controls' GotFocus and LostFocus events to make the control Visible or Collapsed.
There is no toolkit yet which provides watermarked password box. However this may help:-
http://code.msdn.microsoft.com/windowsdesktop/Watermarked-TextBox-and-444ebdec
Also, check out http://julmar.com/blog/mark/?p=300 for both a Textbox and PasswordBox implementation for WinRT.
I am developing an app that displays information while at the same time plays audio file in the background. This is a Windows 8 tablet app. Any help or suggestion is helpful.
Thanks
In the XAML, you will have a MediaElement with the following attributes
<ContentControl x:Name="Host">
<MediaElement Source="Assets/myMusic.mp3" IsLooping="True" AutoPlay="True" AudioCategory="BackgroundCapableMedia" PosterSource="/Images/placeholder-sdk.png" VerticalAlignment="Center" HorizontalAlignment="Center" Name="myMediaElement" Height="350" Width="640" />
</ContentControl>
In the App bar, you can have some buttons for media control like
<Page.BottomAppBar>
<AppBar Opened="BottomAppBar_Opened" Background="#FF3C3838" x:Name="BottomAppBarColors" Padding="10,0,10,0" AutomationProperties.Name="Bottom App Bar Colors">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="LFullScreenLandscapeViewGrid" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
<Button x:Name="Play" Click="PlayButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" Tag="Play"/>
<Button x:Name="Stop" Click="StopButton_Click" Style="{StaticResource StopAppBarButtonStyle}" Tag="Stop"/>
</StackPanel>
<StackPanel Visibility="Collapsed" x:Name="PortraitViewGrid" Orientation="Horizontal" Grid.ColumnSpan="2" HorizontalAlignment="Right">
<Button x:Name="PauseCollapsed" Click="PauseButton_Click" Style="{StaticResource PauseAppBarButtonStyle}" Tag="Pause"/>
<Button x:Name="PlayCollapsed" Click="PlayButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" Tag="Play"/>
<Button x:Name="StopCollapsed" Click="StopButton_Click" Style="{StaticResource StopAppBarButtonStyle}" Tag="Stop"/>
</StackPanel>
</Grid>
</AppBar>
</Page.BottomAppBar>
In the .CS code file, you can control the mediaelement object.
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Pause();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Stop();
}
private void ForwardButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = 2.0;
}
private void RewindButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = -1.0;
}
If you have multiple screens that use the media and controls to play, pause and stop the media file, then it's better to write code for these events once and then use it from different screens. I have seen people writing the following code in every screen they use the media.
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Pause();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Stop();
}
private void ForwardButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = 2.0;
}
private void RewindButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = -1.0;
}
So instead of repeating the code in every screen, follow a good event pattern to reduce duplicate code
The best thing is to use MVVM pattern and you can find a sample at http://code.msdn.microsoft.com/windowsapps/Background-Audio-c-Metro-d2fc7719/view/SourceCode
So the design is to write up the code in one common place and fire them from the front end UI.
You need to use a MediaElement.