I'm trying to use the OxyPlot.Xamarin.Forms package in my shared Xamarin.Forms project. I added the OxyPlot packages with the NuGet package manager in both the portable and the platform specific (Android) projects like descriped at:
http://docs.oxyplot.org/en/latest/getting-started/hello-xamarin-forms.html
Then I initialized the OxyPlot renderers in the Android project. Now, when I try to start the App the Resource.Designer.cs file is generated, but I get hundreds of errors like so:
Error CS0117 'Resource.Attribute' does not contain a definition for 'mediaRouteSettingsDrawable' OxyPlotShared.Droid \OxyPlotShared\OxyPlotShared.Droid\Resources\Resource.Designer.cs
I use Xamarin.Forms v2.2.0.31 and all OxyPlot packages in version 1.0.0-unstable1983.
Am I missing something?
Note that there are 2 different project templates: Portable and Shared. In your post you mention both of them, so be clear about which one you're using. The specific example you're following is for a Portable project template. After adding the OxyPlot Nuget Package, I had to manually add OxyPlot and OxyPlot.Xamarin.Forms to the References of the Portable one. After that, it worked just fine. I'm using Xamarin.Forms 2.0.0.6482 and OxyPlot 1.0.0-unstable1983.
I added the PlotView using code:
App.cs:
public App()
{
PlotModel plotModel = new PlotModel { PlotAreaBackground = OxyColors.LightYellow };
plotModel.Series.Add(new FunctionSeries(Math.Sin, -10, 10, 0.1, "sin(x)"));
// The root page of your application
MainPage = new ContentPage {
Content = new PlotView {
VerticalOptions = LayoutOptions.Fill,
HorizontalOptions = LayoutOptions.Fill,
Model = plotModel
}
};
}
Related
I am working on an application that captures real time pen strokes on a canvas using Wacom Bamboo Slate. The application is being developed for UWP platform using C#. After drawing on the canvas, save feature is to be implemented. I am using this for my reference. Below is the code and error message:
private async void BtnSave_Click(object sender, RoutedEventArgs e)
{
StorageFolder storageFolder = KnownFolders.SavedPictures;
var file = await storageFolder.CreateFileAsync("sample.jpg", CreationCollisionOption.ReplaceExisting);
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.Clear(Colors.White);
ds.DrawInk(inkCanvas.InkPresenter.StrokeContainer.GetStrokes());
}
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);
}
}
CS1061 'InkCanvas' does not contain a definition for InkPresenter and no accessible extension method InkPresenter accepting a first argument of type InkCanvas could be found (are you missing a using directive or an assembly reference?)
Have you considered:
RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.Width, (int)inkCanvas.Height, 96d, 96d, PixelFormats.Default);
rtb.Render(inkCanvas);
After which you can then:
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(fileStream);
The sample referred above can be found here.
The sample use CanvasDevice from the Microsoft.Graphics.Canvas namespace part of the package Win2D.UWP (version 1.6.0) from Microsoft. The UWP project sample targets build 10240 (minimum 10240) of Windows 10.
The package Win2D.UWP can be installed
using the menu "Project > Manage Nuget Packages", or
by selecting the context menu "References" in the UWP project of "Solution Explorer".
Select "Installed" and uninstall the current 2d graphics rendering package, if any.
Select "Browse", look for Win2D.UWP and install the one from Microsoft.
Please note that the latest version of Win2D.UWP updated 5/17/2018 version 1.23.0 requires target platform to be 17134.
For example, "Error List" might show the following error message after a build with version 1.23 of Win2D.UWP and target version set to 10240 in the UWP project properties:
This version of Win2D requires Windows SDK >= 10.0.17134.0, but TargetPlatformVersion is 10.0.10240.0.
Target version can be changed in the UWP project properties
select menu "Project > projectname Properties", or
by selecting the context menu "Properties" from the UWP project in "Solution Explorer".
PS: Add the following after InitializeComponent(); in MainPage.xaml.cs to enable drawing with a selection of input device types:
MyInkCanvas.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Touch;
I follow the guide on the official Xamarin website telling how to embed the image into the cross-platform app. I build the app only for Android platform.
Unfortunately, the image is not shown during debug on my Sony D5803 Z3 Compact or in Android 7.1 emulator. I use VS 2017 RC on Windows 10. All the previous HelloWorld apps e.g. with downloading the picture via URI worked well on my phone.
I would be grateful for any help or advice how to troubleshoot the problem.
The image picture.jpg has set the build property embedded resource (in Polish Akcja kompilacji: osadzony zasób)
I can see the images in the debug folder as shown in the screenshot
This is the XAML page I use to show the image:
ImageEmbeddedPAge.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:markupExtensions="clr-namespace:HelloWorld.MarkupExtensions;assembly=HelloWorld.Droid"
x:Class="HelloWorld.ImageEmbeddedPAge">
<StackLayout HorizontalOptions="Center"
VerticalOptions="Center">
<Image x:Name="image"
Source="{markupExtensions:ImageResource Source=HelloWorld.Images.picture.jpg}"/>
</StackLayout>
</ContentPage>
ImageResourceExtension.cs in folder MarkupExtensions
namespace HelloWorld.MarkupExtensions
{
[ContentProperty("Source")]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source);
return imageSource;
}
}
}
App.xaml.cs
namespace HelloWorld
{
public partial class App : Application
{
public App ()
{
InitializeComponent();
MainPage = new ImageEmbeddedPAge();
}
}
}
Totally agree with #Cheesebaron, since you created a Xamarin.Forms shared projecrt, we need to place resolution images in specially-named directories in Android project, these specially-named directories are all placed under the Resources-> Drawable folder in the Android project.
If you want to use an embedded image, we need to use a Portable Lib for each platfrom, or just create a Xamarin.Forms portable project. If you want to create a PCL by yourself, than please be careful with the targets of this PCL, normally it should be like following image so can it be used for different platforms:
And finally to embed an image in a project, by default the image will have Build Action: None, please set it to Build Action: EmbeddedResource.
The reason why we cannot place images in a shared project is that:
The shared project reference shows up under the References node in the Solution Explorer, but the code and assets in the shared project are treated as if they were files linked into the main project.
The images then are treated as files not resources.
Im new in xamarin studio and im trying to create a cocosproject following the official guide, but this document is not very clear and my project have so many errors.
https://developer.xamarin.com/guides/xamarin-forms/advanced/cocossharp/#nuget
I've created a xamarin.form with IOS, android and PCL as guide say
I've added the cocosSharp packages to IOS and Android projects
BUT
if i don't add the cocosSharp package to PCL target, the cocos Classes cant be found by the code
And if I try to add the cocosSharp packages to PCL, console show this
Could not install package 'CocosSharp 1.7.1'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile259', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
i tried to change the targetFramework but this don't help me
if someone work with cocosSharp and xamarin studio V6, please how can i solve this ?
Or how can i add the add in of cocosSharp in Galery like in previous versions of xamarin ?
This is the code in a ContentPage, Cocos Classes can't be found
public class MyPage : ContentPage
{
public MyPage()
{
Content = new StackLayout
{
Children = {
new Label { Text = "Hello ContentPage" }
}
};
}
void CreateTopHalf(Grid grid)
{
// This hosts our game view.
var gameView = new CocosSharpView()
{
// Notice it has the same properties as other XamarinForms Views
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
// This gets called after CocosSharp starts up:
ViewCreated = HandleViewCreated
};
// We'll add it to the top half (row 0)
grid.Children.Add(gameView, 0, 0);
}
void HandleViewCreated(object sender, EventArgs e)
{
var gameView = sender as CCGameView;
if (gameView != null)
{
// This sets the game "world" resolution to 100x100:
gameView.DesignResolution = new CCSizeI(100, 100);
// GameScene is the root of the CocosSharp rendering hierarchy:
gameScene = new GameScene(gameView);
// Starts CocosSharp:
gameView.RunWithScene(gameScene);
}
}
}
just figured this out myself. It's true that there's no matching build for Profile 259. Try 151 instead:
Right click the PCL project in your solution explorer
Click 'Options'
Click 'General' on the left hand side.
Change the Current Profile to "PCL 4.6 - Profile151"
Click 'OK'.
Now you'll need to refresh the Nuget Packages so that Xamarin Forms can redownload the right profile. Afterward you can successfully add CocosSharp.Forms.
I want to create simple toast notification to action center in windows 10 from this example. But I got problem on Step 2:
using Windows.UI.Notifications;
It`s missing. But I have spent a lot of time to find it and got no result. I really have no idea where I can find or at least download it.
What I tried:
After long search I found Windows.UI.dll in C:\Windows\System32 but when I try to add it as reference into project I got this error. Even after I tried to copy it and made this fully accessible nothing changed
I tried to reinstall .Net (I`m using 4.5.2)
Installed Windows 10 SDK
Tried to import with global
Added
<PropertyGroup>
<TargetPlatformVersion>10.0</TargetPlatformVersion>
</PropertyGroup>
Added System.Runtime.dll reference
Example code which probably is useless for you:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.QueryStringDotNET;
using Windows.UI.Notifications;
namespace MessagerClient.Notifications {
class DefaultWindowsNotification {
public static void notificationTest() {
string title = "Andrew sent you a picture";
string content = "Check this out, Happy Canyon in Utah!";
string image = "http://blogs.msdn.com/something.jpg";
string logo = "ms-appdata:///local/Andrew.jpg";
ToastVisual visual = new ToastVisual() {
BindingGeneric = new ToastBindingGeneric() {
Children =
{
new AdaptiveText()
{
Text = title
},
new AdaptiveText()
{
Text = content
},
new AdaptiveImage()
{
Source = image
}
},
AppLogoOverride = new ToastGenericAppLogo() {
Source = logo,
HintCrop = ToastGenericAppLogoCrop.Circle
}
}
};
Console.WriteLine("NOTIFICATION");
//Can`t use because of Windows.UI library
ToastNotificationManager.CreateToastNotifier().Show(visual);
}
}
}
You have to fight Visual Studio pretty hard to use these UWP contracts in a Winforms app. You got off on the wrong foot right away with the wrong TargetPlatformVersion, pretty hard to recover from that. Full steps to take:
Edit the .csproj file with a text editor, Notepad will do. Insert this:
<PropertyGroup>
<TargetPlatformVersion>10.0.10586</TargetPlatformVersion>
</PropertyGroup>
Which assumes you have the 10586 SDK version installed on your machine. Current right now, these versions change quickly. Double-check by looking in the C:\Program Files (x86)\Windows Kits\10\Include with Explorer, you see the installed versions listed in that directory.
Open the Winforms project, use Project > Add Reference > Windows tab > tick the Windows.Data and the Windows.UI contract. Add Reference again and use the Browse tab to select System.Runtime. I picked the one in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\ .NETFramework\v4.6.1\Facades. This reference displays with a warning icon, not sure what it is trying to say but it doesn't appear to have any side-effects.
Test it by dropping a button on the form, double-click to add the Click event handler. The most basic code:
using Windows.UI.Notifications;
...
private void button1_Click(object sender, EventArgs e) {
var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var text = xml.GetElementsByTagName("text");
text[0].AppendChild(xml.CreateTextNode("Hello world"));
var toast = new ToastNotification(xml);
ToastNotificationManager.CreateToastNotifier("anythinggoeshere").Show(toast);
}
Embellish by using a different ToastTemplateType to add an image or more lines of text. Do keep in mind that your program can only work on a Win10 machine.
If anyone should happen to stumble on this, see this similar but newer post -
Toast Notifications in Win Forms .NET 4.5
Read Stepan Hakobyan's comment at the bottom.
Essentially, I'm seeing the same thing. This code runs, I can step through it line by line with no exceptions but the toast notification is never shown within a Form app.
Background
With Xamarin.Forms 1.3 implicit styles were introduced (jippie). With a Portable Class Library (PCL) as base for your Xamarin.Forms project you can init Application.Current.Resources and add new styles to the collection. I can however not get this working without run time exception.
Collection was modified error
In my App.cs in the Xamarin.Forms PCL project, I've tried adding this code either in the beginning or end as documented:
if (Current.Resources == null)
{
Current.Resources = new ResourceDictionary();
}
Current.Resources.Add(
new Style(typeof(Label))
{
BaseResourceKey = Device.Styles.BodyStyleKey,
Setters =
{
new Setter
{
Property = Label.TextColorProperty,
Value = Color.White
}
}
});
Trying to run this on either Windows Phone or Android gived the exception
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at [...] Xamarin.Forms.Element.OnResourcesChanged(Object sender, ResourcesChangedEventArgs e) at Xamarin.Forms.MergedResourceDictionary...
I've tried this with Xamarin.Forms 1.3 and the latest 1.3.2.6299-pre. No luck.
Anyone seen it, or gotten implicit styles working themselves?