Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OxyPlot.Avalonia user error #59

Open
wwzorz opened this issue Sep 9, 2023 · 20 comments
Open

OxyPlot.Avalonia user error #59

wwzorz opened this issue Sep 9, 2023 · 20 comments

Comments

@wwzorz
Copy link

wwzorz commented Sep 9, 2023

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
Copy link
Contributor

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
Copy link

GoncharovMain commented Sep 15, 2023

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
Copy link
Contributor

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
Copy link
Author

wwzorz commented Sep 17, 2023 via email

@VisualMelon
Copy link
Contributor

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

@wwzorz
Copy link
Author

wwzorz commented Sep 17, 2023 via email

@wwzorz
Copy link
Author

wwzorz commented Sep 17, 2023 via email

@LyubomiRadev
Copy link

LyubomiRadev commented Oct 31, 2023

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
Copy link
Contributor

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
Copy link

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

@VisualMelon
Copy link
Contributor

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
Copy link

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

@VisualMelon
Copy link
Contributor

VisualMelon commented Oct 31, 2023

The line in the csproj is wrong: it should be

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

@LyubomiRadev
Copy link

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
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

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

@LyubomiRadev
Copy link

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
Copy link
Contributor

VisualMelon commented Oct 31, 2023

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
Copy link

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants