Xamarin Forms XAML Image control has quit working - image

I have a working app that displays images. I started a new app and copied the code but this new app does not display images. Puzzled, I have tried many, many things to debug it. I eventually made a plain vanilla new project with only an Image control. As usual, I added the picture to the project using Add Existing Item. I confirmed that the image is there. It does not display the image. I noticed something different in Solution Explorer. Under the Solution, the first project has Dependencies listed under it. This is new. My previous apps have never had that. Now every new project I create has it. I also noticed that the Properties of the image was Build Action of None. I tried changing it to Content but that did not help. At this point, I am baffled and stuck. Did something change in Visual Studio? Here is the image test XAML.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ImageTest"
x:Class="ImageTest.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Image Source="gr8oz.png" />
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
As I said, I have a working app that has been released. I copied this code into a new app and it does not work. In my investigation, I have found that the Image control does not work for me anymore.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:OFamV2"
Style="{StaticResource MyContentPage}"
x:Class="OFamV2.MainPage"
Title="Main Menus">
<ScrollView>
<StackLayout>
<Button x:Name="TreeButton"
Text="Don and Iola Osborne"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="TreeButton_Clicked" />
<Button x:Name="IndianNamesButton"
Text="Indian Names"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="IndianNamesButton_Clicked" />
<Button x:Name="NextPageButton"
Text="Indian Potpourri"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="NextPageButton_Clicked" />
<Button x:Name="OldPhotos"
Text="Old Photos"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="OldPhotos_Clicked" />
<Button x:Name="ReunionButton"
Text="Family Reunion"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="ReunionButton_Clicked"
/>
<StackLayout Orientation="Horizontal">
<Image
Source="Photos/gr8oz.png"
Style="{StaticResource MyImage}"
Margin="0,30,0,0"
Aspect="AspectFit"
HorizontalOptions="End"
/>
<Image
Source = "Photos/logo.bmp"
Style="{StaticResource MyImage}"
Margin="150,0,0,0"
HeightRequest="200"
WidthRequest="200"
HorizontalOptions="End"/>
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage>
I have searched and read everything that I can find (and yes, I have read the Microsoft doc about the Image class).
I am also curious why Dependencies suddenly appeared in the Shared Project folder. This is new too. How do I resolve them? See screenshot.
Dependencies under OFamV2.
I uninstalled Visual Studio, deleted the folder, reinstalled Visual Studio Community 2017 Version 15.8.8 and the problem is still there, even with a new project.
For Options, I select Cross Platform/ Mobile App/ Platform: Android and UWP/Code Sharing: .Net Standard.

Set image show you can see here , i think you have used that.
So reload your project,and rebuild it,you can try this.
More info:
Here is a similar disscussion about image show.
Suggest that better not copy code from other project about view,they may result some problems where you do not know.

Related

Xamarin Forms 5.0 MediaElement was not found

I have a project in xamarin forms 4.6 that updated to xamarin forms 5.0, but when I run the project. This error occurs.
The type 'xct:MediaElement' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have
been built.
I already added this reference xmlns:xct="http://xamarin.com/schemas/2020/toolkit" in my ContentPage but the problem still exist.
here is my code.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="Proyekto4Juan.Views.IndexPage">
<Grid>
<xct:MediaElement x:Name="BgVideo" Source="ms-appx:///video.mp4" ShowsPlaybackControls="False"
IsLooping="True" AutoPlay="True" Aspect="AspectFill"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</Grid
</ContentPage>
Thanks in Advance.
You can recheck if you have installed nuget Xamarin.CommunityToolkit.
And add the following code :
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.UI.Views;assembly=Xamarin.CommunityToolkit"
The Xamarin Community Toolkit is a collection of common elements for mobile development with Xamarin.Forms that people tend to replicate across multiple apps. It simplifies and demonstrates common developer tasks when building apps with Xamarin.Forms.
For more details, you can check document :Xamarin Community Toolkit MediaElement.
You can also refer to the sample code MediaElementDemos.
A usage sample from above sample is AspectVideoPage.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.UI.Views;assembly=Xamarin.CommunityToolkit"
xmlns:controls="clr-namespace:MediaElementDemos.Controls"
x:Class="MediaElementDemos.AspectVideoPage"
Title="Change aspect ratio">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<xct:MediaElement x:Name="mediaElement"
Grid.Row="0"
AutoPlay="False"
ShowsPlaybackControls="True"
Source="{StaticResource AdvancedAsync}" />
<StackLayout Grid.Row="1"
Margin="0,0,0,40"
Orientation="Horizontal"
HorizontalOptions="Center">
<Label Text="Aspect:"
VerticalTextAlignment="Center" />
<controls:EnumPicker x:Name="aspectEnumPicker"
EnumType="{x:Type Aspect}"
SelectedIndex="0"
SelectedIndexChanged="OnAspectSelectedIndexChanged" />
</StackLayout>
</Grid>
</ContentPage>

Xamarin.Forms Images not showing

I am developing a Cross-Platform app in Xamarin Visual Studio 2019 and having issues with image display.
I am trying to show an image with the following XAML code.
<StackLayout Grid.Row="0" Grid.Column="0" Spacing="0" >
<Image
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Source="logo"
WidthRequest="{OnPlatform iOS=300, Android=250}">
</Image>
</StackLayout>
Problem is image not displaying on either preview or deployed app.
I took references from multiple forums including help from StackOverflow but couldn't tackle the issue.
Here are the points and methods I worked on to resolve the issue.
The file named logo is an android resource folder and is a proper .png file.
Property for the file is set to AndroidResource.
As per answers posted here, Xamarin Images not showing, file name does not have a hyphen in it, and also tried other posted answers on the same page.
Image size 307X80 pixels so it's well within range for memory or size.
Android Drawable folder also contain default xamarin_logo.png image and that file works fine. But not newly added images. Is there anything I am missing here to add these image files to the solution?
Either the image extension or image path or the build action for the image seems to be the problem in your case. I tried your code and got that working fine for both png and jpg images.
<StackLayout Spacing="0">
<Image HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
Source="github.png" WidthRequest="{OnPlatform iOS=300, Android=250}" />
<Image HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
Source="githubb.jpg" WidthRequest="{OnPlatform iOS=300, Android=250}" />
</StackLayout>
Note: I have removed the grid.row and grid.column indexing as I am not using my StackLayout inside a grid.
Please make sure whether you have kept your images directly inside the drawable folder in the Resources in Android. If not, you may have to give the Source name as FolderName\ImageName.Extension.
Refer below link for more details:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#local-images
Also, please ensure whether the BuildAction of your image is AndroidResource and Custom Tool is MSBuild:UpdateGeneratedFiles. Please refer to the below image.
I got the below output for the code which I posted above.
I hope that helps.
Your XAML code is Correct but you have missed the image extension .png you must define your image Extension(format) Ex: (image.png or image.jpeg) That is the reason the image is not displayed.
<StackLayout Grid.Row="0" Grid.Column="0" Spacing="0" >
<Image
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Source="logo.png"
WidthRequest="{OnPlatform iOS=300, Android=250}">
</Image>
</StackLayout>

The Type "name" was not found - Xamarin

I am trying to implemenat a TabbedPage with Xamarin Forms. It is very similar to TabbedPage and Content pages are not showing
However I have a quick question as to how to include other xaml files from the MainPage.xam:
Here is my code:
MainPage:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Assignment"
x:Class="Assignment.MainPage">
<TabbedPage.Children>
<local:Alarm /> "The type 'local:Alarm' was not found. Verify that you are not missing..."
</TabbedPage.Children>
The above error is the only build error.
Alarm.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Assignment.Droid.Resources.layout.Alarm">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Here is my project structure:
On the other link, I saw that he put his xaml files in the layout folder.
Is this the correct setup? I think I have missed something simple.
As Greggz mentions: move it to your shared project.
Then you will have to change your namespaces because that is what actually matters. In .NET land, it is customary that your namespace equals the path on your filesystem.
In you Alarm.xaml, change this line: x:Class="Assignment.Droid.Resources.layout.Alarm" to x:Class="Assignment.Alarm".
Then go into your code-behind, the Alarm.xaml.cs file and change the namespace there too. So change namespace Assignment.Droid.Resources.layout to namespace Assignment.
Since the XAML and the code-behind are implemented as a partial class, this needs to be in sync.

How to (or why can't I) add a plain .xaml file to my Visual Studio solution and edit it within?

Status
I have the following (Visual Studio 2017) solution configuration:
created a new solution with one .csproject for post-build commands
added several other .csprojects to the solution via add existing project
now I want to add a plain .xaml file, which works via add an existing item to the solution, but throws the error:
Object reference not set to an instance of an object.
Problem
The item still appears, but a double-click to open it throws the same error and does not let me edit it.
Unwanted fixes
I can prevent this error by two ways (which I do not want):
rename the .xaml files to e.g. a .xaml.txt file ending (and name them back within the post-solution-build commands)
put the .xaml files into an own .csproj folder with a .cs file only defining the empty namespace ThisIsMyGraphicsElement { } and a Properties\AssemblyInfo.cs file
So my question is
How can I add and edit a plain .xaml file to my Visual Studio solution?
Edit
Here is an example code:
```
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="410" d:DesignWidth="800">
<Grid Background="#FFe5e5e6">
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Left" Height="200" VerticalAlignment="Top" Width="500" Margin="150,140,0,0" Grid.RowSpan="2">
<TextBox x:Name="textBox1" HorizontalAlignment="Center" Height="100" TextWrapping="Wrap" VerticalAlignment="Top" Width="366.865" Margin="0" Background="{x:Null}" FontSize="24" Text="Live happily ever after...
" BorderBrush="{x:Null}"/>
</StackPanel>
</Grid>
</UserControl>
```
As mentioned in comments/answers, adding a new (empty) .xaml file and pasting the code did not work as well.
Not sure what is happening here. You could try to create a new XAML within your project and then paste the XAML from the file you want to include. That's what I would try.
Edit: Not being able to comment.... I'm not the first to suggest this.

Windows phone ads not working

I am trying to integrate ads into a already successful deployed app. However no matter what I do, I cannot seem to get the ads working. I have tried using both the code version and the drag n' drop gui version. Neither of which I can get to work.
This is what I see: When it starts up it may flash for a split second white, where the ad is supposed to be, but none the less, no adds. It recognizes that it is where I place it, when I place it over a button, the button becomes unclickable. All being said, no default "microsoft advertising" image pops up. I have installed the ad SDK and have successfully been able to make the ads display in other project with ease.
What gives? This is very simple page and I cannot figure out what is wrong. It also seems that I cannot place an ad on any of the other pages either... I do have the Microsoft.Advertising.Mobile and Microsoft.Advertising. Mobile.UI included in the project and my internet is working (I have an project open at the same time with ads and it works)
<phone:PhoneApplicationPage
x:Class="AppName.AdPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"
xmlns:my="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Margin="12,17,12,28">
<TextBlock x:Name="PageTitle" Text="Thank You!" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Width="334" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0" Height="569" VerticalAlignment="Top">
<Button Content="Ok" Height="72" HorizontalAlignment="Center" Margin="0,428,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
<my:AdControl AdUnitId="Image480_80" ApplicationId="test_client" Height="80" HorizontalAlignment="Left" Margin="-12,458,0,0" Name="adControl1" VerticalAlignment="Top" Width="480" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Advertising.Mobile.UI;
using Microsoft.Advertising.Mobile;
namespace Stickey_Note_v._1
{
public partial class AdPage : PhoneApplicationPage
{
public AdPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}
}
I had the same issue and wrote a blog post about it. Here's the important stuff:
The symptoms of a problem with the SDK's AdControl seem to be pretty consistent: The page loads, the control flickers briefly, showing the hint of a 1 pixel frame, and then, poof. It collapses into nothingness, leaving only a black hole of dispair.
In theory, setting up the AdControl is simple. The documentation from Microsoft outlines the basics:
Download and install the Microsoft Advertising SDK.
Add a reference to Microsoft.Advertising.Mobile.UI.
Drag the control onto the page in the Visual Studio designer.
Set the AdUnitId and ApplicationId properties to either test values or actual live values, which you can obtain from Microsoft pubCenter.
But it couldn't be that easy. I followed the documentation carefully, but nothing was working. I couldn't even get test ads to show up, which seemed really weird. I even reverted to an older version of my app (yay source control!) and dropped in the new .dll. Failure.
Finally, I found a clue in an obscure forum post.
The Microsoft documentation neglects to mention several important details. You need to pay particular attention to the following if you're upgrading an existing project to the Mango ad SDK, as I was:
You must specify a height and width for the AdControl. Failing to specify the Height and Width attributes, or setting them to auto, will cause tears of frustration. I'd recommend 80 pixels high and 480 pixels wide, as that's the native size of the ads that Microsoft serves up.
It seems that you can't have two AdControls on the same page, or at least not in the same parent element. The second one will collapse. There might be a way around this, but I discovered it while building my demo app and didn't care to pursue a solution.
You must must must specify certain capabilities in your WMAppManifest.xml file. Since I was upgrading my app, I didn't have some of the newer capabilities declared. The one that was causing all the trouble was ID_CAP_IDENTITY_USER. The following capabilities are all required for the control to function correctly:
<Capabilities>
<Capability Name="ID_CAP_IDENTITY_USER"/>
<Capability Name="ID_CAP_MEDIALIB"/>
<Capability Name="ID_CAP_NETWORKING"/>
<Capability Name="ID_CAP_PHONEDIALER"/>
<Capability Name="ID_CAP_WEBBROWSERCOMPONENT"/>
</Capabilities>
Hope that helps!
I think your problem could come from (if you didn't remove it for posting it here):
<my:AdControl AdUnitId="Image480_80" ApplicationId="test_client" Height="80" HorizontalAlignment="Left" Margin="-12,458,0,0" Name="adControl1" VerticalAlignment="Top" Width="480" />
</Grid>
You have to use AdUnitId and ApplicationId which you get from pubCenter.
One more think to note for those still struggling
xmlns:ads="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
<ads:AdControl Height="80"
Width="480"
AdUnitId="Image480_80"
ApplicationId="test_client" />
As in example. Declaring ads and the namespace is super important.
In my project i just inserted the adcontrol part alt + enter for resharper to do the rest of the work which he didnt. He replaces ads with UI and there was no error compilation still the ads wouldn't show. When i declared ads myself and changed the adcontrol namespace. Everything worked fine.

Resources