oxyplot / oxyplot-avalonia

A cross-platform plotting library for .NET. This package targets Avalonia apps.
https://oxyplot.github.io/
MIT License
278 stars 47 forks source link

OxyPlot.Avalonia user error #59

Open wwzorz opened 1 year ago

wwzorz commented 1 year ago
Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:AvaloniaApplication1.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:views="clr-namespace:AvaloniaApplication1.Views"
        xmlns:oxy="using:OxyPlot.Avalonia"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaApplication1.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="AvaloniaApplication1">
  <views:MainView>

    <TabControl>
      <Grid>
        <oxy:PlotView></oxy:PlotView>
      </Grid>
    </TabControl>
  </views:MainView>
</Window

build error:

[Error] 16196 UpdateXamlResult error
System.Xaml.XamlException: “Unable to resolve type PlotView from namespace using:OxyPlot.Avalonia Line 16, position 10.”,行号为“16”,行位置为“10”。

runtime error

System.IO.FileNotFoundException:“Could not load file or assembly 'Avalonia.Visuals, Version=0.10.11.0, Culture=neutral, PublicKeyToken=c8d484a7012f9a8b'. 系统找不到指定的文件。”

请问下为什么会出现编译和运行时错误
VisualMelon commented 1 year ago

That should work: which OxyPlot.Avalonia package are you targetting and have you tried forcing a restore from the command line with dotnet restore? (VS certainly used to mess up sometimes in this regard)

GoncharovMain commented 1 year ago

This is not a solution, but for version template 0.10.18:

dotnet new install Avalonia.Templates::0.10.18
dotnet new avalonia.app

You can use the Content property. MainWindow.axaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        PlotView plotView = new()
        {
            Model = _model,
            Width = 400,
            Height = 300
        };

        Grid grid = (Grid)Content;

        grid.Children.Add(plotView);
    }
}

From https://github.com/oxyplot/oxyplot-avalonia App.axaml

<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="App1.App">
    <Application.Styles>
    <StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
    <StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseLight.xaml"/>
    <StyleInclude Source="resm:OxyPlot.Avalonia.Themes.Default.xaml?assembly=OxyPlot.Avalonia"/>
    </Application.Styles>
</Application>

MainWindow.axaml

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="250"
        x:Class="App1.MainWindow"
        Title="App1">
    <Grid>

    </Grid>
</Window>
VisualMelon commented 1 year ago

Not sure what's going on there... the code-behind shouldn't be necessary: I've just installed the same template, created the project, added the nuget reference and defined a PlotView in sample and it 'just works'. Package reference is

<PackageReference Include="OxyPlot.Avalonia" Version="2.1.0" />

The style include is necessary for the plot to appear, but there shouldn't be an error without it.

My complete example code: https://gist.github.com/VisualMelon/26f19b2a9fed7adf41fe121d7925e49e

wwzorz commented 1 year ago

thank you for reply,this is a version question,use the lower avaliona template can be solved this error!发自我的 iPhone在 2023年9月16日,01:01,VisualMelon @.***> 写道: Not sure what's going on there... the code-behind shouldn't be necessary: I've just installed the same template, created the project, added the nuget reference and defined a PlotView in sample and it 'just works'. Package reference is

The style include is necessary for the plot to appear, but there shouldn't be an error without it. My complete example code: https://gist.github.com/VisualMelon/26f19b2a9fed7adf41fe121d7925e49e

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

VisualMelon commented 1 year ago

OK. If you are targeting Avalonia 11, you should use the 2.1.0-Avalonia11 nuget version of OxyPlot.Avalonia

wwzorz commented 1 year ago

thanks,you are right,I am using the latest version and have already resolved this issue using a lower version发自我的 iPhone在 2023年9月16日,01:01,VisualMelon @.***> 写道: Not sure what's going on there... the code-behind shouldn't be necessary: I've just installed the same template, created the project, added the nuget reference and defined a PlotView in sample and it 'just works'. Package reference is

The style include is necessary for the plot to appear, but there shouldn't be an error without it. My complete example code: https://gist.github.com/VisualMelon/26f19b2a9fed7adf41fe121d7925e49e

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

wwzorz commented 1 year ago

Because this is the first time I have been exposed to cross platform solutions, the frameworks have been rebuilt and the latest versions have been used. I didn't pay much attention to version issues. Thank you again发自我的 iPhone在 2023年9月17日,22:13,VisualMelon @.***> 写道: OK. If you are targeting Avalonia 11, you should use the 2.1.0-Avalonia11 nuget version of OxyPlot.Avalonia

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

LyubomiRadev commented 1 year ago

I am having the same issue. When I try to use oxyPlot I get an erro: Error: Unable to resolve type Plot from namespace using:OxyPlot.Avalonia Line 11, position 10. I tried to include the style as mentioned above with resm or avares but I get an error as well( Error: Unable to resolve XAML resource "avares://AvaloniaApplication1/Designer/OxyPlot.Avalonia.Themes.Default.xaml? assembly=OxyPlot.Avalonia" in the "AvaloniaApplication1" assembly. Make sure this file exists and is public. Line 9, position 23.). I am using the latest version of Avalonia and the .1.0-Avalonia11 nuget version of OxyPlot.Avalonia.

You can find my code here . I am using the latest version of Avalonia with OxyPlot.Avalonia 2.1.0.

VisualMelon commented 1 year ago

The link 404s for me. Double check that a restore has been run: VS can get confused sometimes, and running dotnet restore manually can resolve that.

LyubomiRadev commented 1 year ago

Sorry, the repo was not public. You can try again. I tired running dotnet restore but it did not work.

VisualMelon commented 1 year ago

https://github.com/LyubomiRadev/PieChart/blob/master/AvaloniaApplication1/AvaloniaApplication1.csproj#L21C5-L21C68

This should be referencing version 2.1.0-Avalonia11 because you are using Avalonia 11 (per https://github.com/LyubomiRadev/PieChart/blob/master/Directory.Build.props#L4 )

LyubomiRadev commented 1 year ago

So if I understand correctly it should be working, right? So why it isn't :)

VisualMelon commented 1 year ago

The line in the csproj is wrong: it should be

<PackageReference Include="OxyPlot.Avalonia" Version="2.1.0-Avalonia11" />

LyubomiRadev commented 1 year ago

Thank you. That really helped. But now I can't seem to get it working properly. Nothing is displayed in the window. Can you take a look and tell me what I am doing wrong?

VisualMelon commented 1 year ago

You've commented out the style inclue: https://github.com/LyubomiRadev/PieChart/blob/master/AvaloniaApplication1/App.axaml#L9C8-L9C110 Without the styles, the PlotView won't have a visual presence.

LyubomiRadev commented 1 year ago

When I include the Style I get an error: No precompiled XAML found for resm:OxyPlot.Avalonia.Themes.Default.xaml?assembly=OxyPlot.Avalonia (baseUri: avares://AvaloniaApplication1/App.axaml), make sure to specify x:Class and include your XAML file as AvaloniaResource. That is why it was commented.

VisualMelon commented 1 year ago

Oh yes, that's the old one. With Avalonia11 you need this style include: <StyleInclude Source="avares://OxyPlot.Avalonia/Themes/Default.axaml"/>

LyubomiRadev commented 1 year ago

Fantastic! No building errors whatsoever. But I am not able to display the chart. All I see is the Title of the chart that I've put. I have created the Continents collection as in the example, but I am not able to display the chart. Is the view not detecting the changes in the collection? Also you say that the StyleInclude I was using was the old one. Where can I get the latest updates? I looked in the documentation but I saw information only about WPF, UWP, WinForms etc. but no Avalonia.

VisualMelon commented 1 year ago

The documentation is pretty patchy, and none of it applies to the Avalonia provision.

You are using compiled bindings, and then using an annoumous type as your data-context: this is asking for trouble. It seems to work fine if you replace it with this.DataContext = this

https://github.com/LyubomiRadev/PieChart/blob/master/AvaloniaApplication1/Views/MainWindow.axaml.cs#L40

Then it will match the declared type

https://github.com/LyubomiRadev/PieChart/blob/master/AvaloniaApplication1/Views/MainWindow.axaml#L10

LyubomiRadev commented 1 year ago

NICE! That work. Thank you for all your help. I am quite new to Avalonia and am not familiar with a lot of things, which is weird, because I worked with WPF for 7 years 😄 Anyway thank you very much :)